You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`useQuery` and all its corresponding specialized hooks support an `enabled` option. This is useful if you need to have a query executed only when a condition is met. For example, in the following example, we only fetch the categories if we have at least one post:
// run only if the first query returns non-empty result
427
+
{ enabled:ids.length>0 }
428
+
);
429
+
```
430
+
411
431
## Handling Side Effects In `useDataProvider`
412
432
413
433
`useDataProvider` returns a `dataProvider` object. Each call to its method return a Promise, allowing adding business logic on success in `then()`, and on failure in `catch()`.
Copy file name to clipboardexpand all lines: packages/ra-core/src/dataProvider/useGetList.ts
+6-2
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,7 @@ import {
8
8
Identifier,
9
9
Record,
10
10
RecordMap,
11
+
UseDataProviderOptions,
11
12
}from'../types';
12
13
importuseQueryWithStorefrom'./useQueryWithStore';
13
14
@@ -31,7 +32,10 @@ const defaultData = {};
31
32
* @param {Object} pagination The request pagination { page, perPage }, e.g. { page: 1, perPage: 10 }
32
33
* @param {Object} sort The request sort { field, order }, e.g. { field: 'id', order: 'DESC' }
33
34
* @param {Object} filter The request filters, e.g. { title: 'hello, world' }
34
-
* @param {Object} options Options object to pass to the dataProvider. May include side effects to be executed upon success or failure, e.g. { onSuccess: { refresh: true }}
35
+
* @param {Object} options Options object to pass to the dataProvider.
36
+
* @param {boolean} options.enabled Flag to conditionally run the query. If it's false, the query will not run
37
+
* @param {Function} options.onSuccess Side effect function to be executed upon success, e.g. { onSuccess: { refresh: true }}
38
+
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) }
35
39
*
36
40
* @returns The current request state. Destructure as { data, total, ids, error, loading, loaded }.
* @param ids The resource identifiers, e.g. [123, 456, 789]
62
-
* @param options Options object to pass to the dataProvider. May include side effects to be executed upon success or failure, e.g. { onSuccess: { refresh: true }}
67
+
* @param {Object} options Options object to pass to the dataProvider.
68
+
* @param {boolean} options.enabled Flag to conditionally run the query. If it's false, the query will not run
69
+
* @param {Function} options.onSuccess Side effect function to be executed upon success, e.g. { onSuccess: { refresh: true }}
70
+
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) }
63
71
*
64
72
* @returns The current request state. Destructure as { data, error, loading, loaded }.
Copy file name to clipboardexpand all lines: packages/ra-core/src/dataProvider/useGetManyReference.ts
+12-2
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,13 @@ import {
18
18
constdefaultIds=[];
19
19
constdefaultData={};
20
20
21
+
interfaceUseGetManyReferenceOptions{
22
+
onSuccess?: (args?: any)=>void;
23
+
onFailure?: (error: any)=>void;
24
+
enabled?: boolean;
25
+
[key: string]: any;
26
+
}
27
+
21
28
/**
22
29
* Call the dataProvider.getManyReference() method and return the resolved result
23
30
* as well as the loading state.
@@ -38,7 +45,10 @@ const defaultData = {};
38
45
* @param {Object} sort The request sort { field, order }, e.g. { field: 'id', order: 'DESC' }
39
46
* @param {Object} filter The request filters, e.g. { body: 'hello, world' }
40
47
* @param {string} referencingResource The resource name, e.g. 'posts'. Used to generate a cache key
41
-
* @param {Object} options Options object to pass to the dataProvider. May include side effects to be executed upon success or failure, e.g. { onSuccess: { refresh: true }}
48
+
* @param {Object} options Options object to pass to the dataProvider.
49
+
* @param {boolean} options.enabled Flag to conditionally run the query. If it's false, the query will not run
50
+
* @param {Function} options.onSuccess Side effect function to be executed upon success, e.g. { onSuccess: { refresh: true }}
51
+
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) }
42
52
*
43
53
* @returns The current request state. Destructure as { data, total, ids, error, loading, loaded }.
* @param {Object} filter The request filters, e.g. { title: 'hello, world' }
42
49
* @param {string} source The field in resource containing the ids of the referenced records, e.g. 'tag_ids'
43
50
* @param {string} referencingResource The resource name, e.g. 'posts'. Used to build a cache key
44
-
* @param {Object} options Options object to pass to the dataProvider. May include side effects to be executed upon success or failure, e.g. { onSuccess: { refresh: true }}
51
+
* @param {Object} options Options object to pass to the dataProvider.
52
+
* @param {boolean} options.enabled Flag to conditionally run the query. If it's false, the query will not run
53
+
* @param {Function} options.onSuccess Side effect function to be executed upon success, e.g. { onSuccess: { refresh: true }}
54
+
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) }
45
55
*
46
56
* @returns The current request state. Destructure as { data, total, ids, error, loading, loaded }.
@@ -18,7 +23,10 @@ import useQueryWithStore from './useQueryWithStore';
18
23
*
19
24
* @param resource The resource name, e.g. 'posts'
20
25
* @param id The resource identifier, e.g. 123
21
-
* @param options Options object to pass to the dataProvider. May include side effects to be executed upon success or failure, e.g. { onSuccess: { refresh: true }}
26
+
* @param {Object} options Options object to pass to the dataProvider.
27
+
* @param {boolean} options.enabled Flag to conditionally run the query. If it's false, the query will not run
28
+
* @param {Function} options.onSuccess Side effect function to be executed upon success, e.g. { onSuccess: { refresh: true }}
29
+
* @param {Function} options.onFailure Side effect function to be executed upon failure, e.g. { onFailure: error => notify(error.message) }
22
30
*
23
31
* @returns The current request state. Destructure as { data, error, loading, loaded }.
24
32
*
@@ -36,7 +44,7 @@ import useQueryWithStore from './useQueryWithStore';
0 commit comments