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
You can incrementally adopt `jotai-tanstack-query` in your app. It's not an all or nothing solution. You just have to ensure you are using the same QueryClient instance. [QueryClient Setup](#referencing-the-same-instance-of-query-client-in-your-project).
39
+
You can incrementally adopt `jotai-tanstack-query` in your app. It's not an all or nothing solution. You just have to ensure you are using the same QueryClient instance. [same QueryClient](#referencing-the-same-instance-of-query-client).
### Referencing the same instance of Query Client in your project
365
+
### Referencing the same instance of Query Client
367
366
368
-
Perhaps you have some custom hooks in your project that utilises the `useQueryClient()` hook to obtain the `QueryClient` object and call its methods.
367
+
Perhaps you have some custom hooks in your project that utilizes the `useQueryClient()` hook to obtain the `QueryClient` object and call its methods.
369
368
370
-
To ensure that you reference the same `QueryClient` object, be sure to wrap the root of your project in a `<Provider>` and initialise`queryClientAtom` with the same `queryClient` value you provided to `QueryClientProvider`.
369
+
To ensure that you reference the same `QueryClient` object, be sure to wrap the root of your project in a `<Provider>` and initialize`queryClientAtom` with the same `queryClient` value you provided to `QueryClientProvider`.
371
370
372
-
Without this step, `useQueryAtom` will reference a separate `QueryClient` from any hooks that utilise the `useQueryClient()` hook to get the queryClient.
371
+
Without this step, `useQueryAtom` will reference a separate `QueryClient` from any hooks that utilize the `useQueryClient()` hook to get the queryClient.
373
372
374
373
Alternatively, you can specify your `queryClient` with `getQueryClient` parameter.
375
374
376
375
#### Example
377
376
378
377
In the example below, we have a mutation hook, `useTodoMutation` and a query `todosAtom`.
379
378
380
-
We included an initialisation step in our root `<App>` node.
379
+
We included an initialization step in our root `<App>` node.
381
380
382
-
Although they reference methods same query key (`'todos'`), the `onSuccess` invalidation in `useTodoMutation` will not trigger **if the `Provider`initialisation step was not done.**
381
+
Although they reference methods same query key (`'todos'`), the `onSuccess` invalidation in `useTodoMutation` will not trigger **if the `Provider`initialization step was not done.**
383
382
384
383
This will result in `todosAtom` showing stale data as it was not prompted to refetch.
385
384
@@ -405,12 +404,16 @@ export const Root = () => {
405
404
)
406
405
}
407
406
408
-
exportconsttodosAtom=atomWithQuery((get) => {
409
-
return {
410
-
queryKey: ['todos'],
411
-
queryFn: () =>fetch('/todos'),
412
-
}
413
-
})
407
+
exportconsttodosAtom=atomWithQuery(
408
+
(get) => {
409
+
return {
410
+
queryKey: ["todos"],
411
+
queryFn: () =>fetch("/todos"),
412
+
}
413
+
},
414
+
() => queryClient
415
+
)
416
+
414
417
415
418
exportconstuseTodoMutation= () => {
416
419
constqueryClient=useQueryClient()
@@ -445,11 +448,7 @@ See [a working example](https://codesandbox.io/s/4gfp6z) to learn more.
445
448
In order to use the Devtools, you need to install it additionally.
446
449
447
450
```bash
448
-
$ npm i @tanstack/react-query-devtools
449
-
# or
450
-
$ pnpm add @tanstack/react-query-devtools
451
-
# or
452
-
$ yarn add @tanstack/react-query-devtools
451
+
$ npm i @tanstack/react-query-devtools --save-dev
453
452
```
454
453
455
454
All you have to do is put the `<ReactQueryDevtools />` within `<QueryClientProvider />`.
0 commit comments