Skip to content

Commit 78847bc

Browse files
authored
Merge pull request #7704 from marmelab/fix-fetchonwindowrefocus-documentation$
[Doc] Fix missing doc for `<Admin queryClient>` prop
2 parents 271f3d1 + ba990e3 commit 78847bc

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

docs/Admin.md

+76
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Here are all the props accepted by the component:
3232
- [`dataProvider`](#dataprovider)
3333
- [`authProvider`](#authprovider)
3434
- [`i18nProvider`](#i18nprovider)
35+
- [`queryClient`](#queryclient)
3536
- [`title`](#title)
3637
- [`dashboard`](#dashboard)
3738
- [`disableTelemetry`](#disabletelemetry)
@@ -95,6 +96,81 @@ The [Auth Provider documentation](./Authentication.md) explains how to implement
9596

9697
The `i18nProvider` props let you translate the GUI. The [Translation Documentation](./Translation.md) details this process.
9798

99+
## `queryClient`
100+
101+
React-admin uses [react-query](https://react-query.tanstack.com/) to fetch, cache and update data. Internally, the `<Admin>` component creates a react-query [`QueryClient`](https://react-query.tanstack.com/reference/QueryClient) on mount, using [react-query's "aggressive but sane" defaults](https://react-query.tanstack.com/guides/important-defaults):
102+
103+
* Queries consider cached data as stale
104+
* Stale queries are refetched automatically in the background when:
105+
* New instances of the query mount
106+
* The window is refocused
107+
* The network is reconnected
108+
* The query is optionally configured with a refetch interval
109+
* Query results that are no longer used in the current page are labeled as "inactive" and remain in the cache in case they are used again at a later time.
110+
* By default, "inactive" queries are garbage collected after 5 minutes.
111+
* Queries that fail are silently retried 3 times, with exponential backoff delay before capturing and displaying an error to the UI.
112+
* Query results by default are structurally shared to detect if data has actually changed and if not, the data reference remains unchanged to better help with value stabilization with regards to `useMemo` and `useCallback`.
113+
114+
If you want to override the react-query default query and mutation default options, or use a specific client or mutation cache, you can create your own `QueryClient` instance and pass it to the `<Admin queryClient>` prop:
115+
116+
```jsx
117+
import { Admin } from 'react-admin';
118+
import { QueryClient } from 'react-query';
119+
120+
const queryClient = new QueryClient({
121+
defaultOptions: {
122+
queries: {
123+
retry: false,
124+
structuralSharing: false,
125+
},
126+
mutations: {
127+
retryDelay: 10000,
128+
},
129+
},
130+
});
131+
132+
const App = () => (
133+
<Admin queryClient={queryClient} dataProvider={...}>
134+
...
135+
</Admin>
136+
);
137+
```
138+
139+
To know which options you can pass to the `QueryClient` constructor, check the [react-query documentation](https://react-query.tanstack.com/reference/QueryClient) and the [query options](https://react-query.tanstack.com/reference/useQuery) and [mutation options](https://react-query.tanstack.com/reference/useMutation) sections.
140+
141+
The common settings that react-admin developers often overwrite are:
142+
143+
```jsx
144+
import { QueryClient } from 'react-query';
145+
146+
const queryClient = new QueryClient({
147+
defaultOptions: {
148+
queries: {
149+
/**
150+
* The time in milliseconds after data is considered stale.
151+
* If set to `Infinity`, the data will never be considered stale.
152+
*/
153+
staleTime: 10000,
154+
/**
155+
* If `false`, failed queries will not retry by default.
156+
* If `true`, failed queries will retry infinitely., failureCount: num
157+
* If set to an integer number, e.g. 3, failed queries will retry until the failed query count meets that number.
158+
* If set to a function `(failureCount, error) => boolean` failed queries will retry until the function returns false.
159+
*/
160+
retry: false,
161+
/**
162+
* If set to `true`, the query will refetch on window focus if the data is stale.
163+
* If set to `false`, the query will not refetch on window focus.
164+
* If set to `'always'`, the query will always refetch on window focus.
165+
* If set to a function, the function will be executed with the latest data and query to compute the value.
166+
* Defaults to `true`.
167+
*/
168+
refetchOnWindowFocus: false,
169+
},
170+
},
171+
});
172+
```
173+
98174
## `title`
99175

100176
On error pages, the header of an admin app uses 'React Admin' as the main app title. Use the `title` to customize it.

docs/DataProviders.md

+26
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,32 @@ export default App;
365365

366366
**Tip**: This example uses the function version of `setState` (`setDataProvider(() => dataProvider))`) instead of the more classic version (`setDataProvider(dataProvider)`). This is because some legacy Data Providers are actually functions, and `setState` would call them immediately on mount.
367367

368+
## Default Query Options
369+
370+
If you often need to pass the same query options to the data provider, you can use [the `<Admin queryClient>` prop](./Admin.md#queryclient) to set them globally.
371+
372+
```jsx
373+
import { Admin } from 'react-admin';
374+
import { QueryClient } from 'react-query';
375+
376+
const queryClient = new QueryClient({
377+
defaultOptions: {
378+
queries: {
379+
retry: false,
380+
staleTime: Infinity,
381+
},
382+
}
383+
});
384+
385+
const App = () => (
386+
<Admin queryClient={queryClient} dataProvider={...}>
387+
...
388+
</Admin>
389+
);
390+
```
391+
392+
To know which query options you can override, check the [Querying the API documentation](./Actions.md#query-options) and [the `<Admin queryClient>` prop documentation](./Admin.md#queryclient).
393+
368394
## Combining Data Providers
369395

370396
If you need to build an app relying on more than one API, you may face a problem: the `<Admin>` component accepts only one `dataProvider` prop. You can combine multiple data providers into one using the `combineDataProviders` helper. It expects a function as parameter accepting a resource name and returning a data provider for that resource.

0 commit comments

Comments
 (0)