DataQuery Component
A thin wrapper around the useDataQuery hook
Basic Usage
import { DataQuery } from '@dhis2/app-runtime'
// within a React component's JSX render tree
return (
<DataQuery query={query}>
{({ loading, error, data, refetch }) => {
/* child render funtion */
}}
</DataQuery>
)
Input Props
Name | Type | Required | Description |
---|---|---|---|
query | Query | required | The Query definition describing the requested data |
variables | Object | Variables to be passed to the dynamic portions of the query | |
onComplete | Function | Callback function to be called on successfull completion of the query. Called with the response data as the only argument. | |
onError | Function | Callback function to be called on failure of the query. Called with the error instance as the only argument. | |
lazy | boolean | If true, wait until refetch is called before fetching data.Default: false |
Render Function Props
Name | Type | Description |
---|---|---|
called | boolean | true if the data request has been initiated with either lazy: false or refetch() at least once |
loading | boolean | true if the data is not yet available and no error has yet been encountered |
error | Error or undefined | undefined if no error has occurred, otherwise the Error which was thrown |
data | QueryResult or undefined | undefined if the data is loading or an error has occurred, otherwise a map from the name of each QueryDefinition defined in the Query to the resulting data for that query |
refetch | Function | This function can be called to refetch the data. Any in-flight HTTP requests will automatically be aborted. |
engine | Data Engine | A reference to the DataEngine instance |
Example
import React from 'react'
import { DataQuery } from '@dhis2/app-runtime'
const query = {
indicators: {
resource: 'indicators',
params: ({ count }) => ({
order: 'shortName:desc',
pageSize: count,
})
},
}
export const IndicatorList = ({ count }) => (
<DataQuery query={query} variables={{ count }}>
{({ loading, error, data }) => (
<h3>Indicators (first {count})</h3>
{loading && <span>...</span>}
{error && <span>{`ERROR: ${error.message}`}</span>}
{data && (
<pre>
{data.indicators.indicators
.map(ind => ind.displayName)
.join('\n')}
</pre>
)}
)}
</DataQuery>
)