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. [same QueryClient](#referencing-the-same-instance-of-query-client).
72
+
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](#exported-provider).
[](https://stackblitz.com/github/jotaijs/jotai-tanstack-query/tree/main/examples/08_query_client_atom_provider)
92
+
93
+
`QueryClientAtomProvider` is a ready-to-use wrapper that combines Jotai Provider and TanStack Query QueryClientProvider.
-`atomWithQuery` for [useQuery](https://tanstack.com/query/v5/docs/react/reference/useQuery)
@@ -363,76 +448,6 @@ const Posts = () => {
363
448
}
364
449
```
365
450
366
-
### Referencing the same instance of Query Client
367
-
368
-
Perhaps you have some custom hooks in your project that utilizes the `useQueryClient()` hook to obtain the `QueryClient` object and call its methods.
369
-
370
-
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
-
372
-
Without this step, `useQueryAtom` will reference a separate `QueryClient` from any hooks that utilize the `useQueryClient()` hook to get the queryClient.
373
-
374
-
Alternatively, you can specify your `queryClient` with `getQueryClient` parameter.
375
-
376
-
#### Example
377
-
378
-
In the example below, we have a mutation hook, `useTodoMutation` and a query `todosAtom`.
379
-
380
-
We included an initialization step in our root `<App>` node.
381
-
382
-
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
-
384
-
This will result in `todosAtom` showing stale data as it was not prompted to refetch.
awaitfetch('/todo', { Method:'POST', Body: body })
425
-
},
426
-
{
427
-
onSuccess: () => {
428
-
voidqueryClient.invalidateQueries(['todos'])
429
-
},
430
-
onError,
431
-
}
432
-
)
433
-
}
434
-
```
435
-
436
451
### SSR support
437
452
438
453
All atoms can be used within the context of a server side rendered app, such as a next.js app or Gatsby app. You can [use both options](https://tanstack.com/query/v5/docs/guides/ssr) that React Query supports for use within SSR apps, [hydration](https://tanstack.com/query/v5/docs/react/guides/ssr#using-the-hydration-apis) or [`initialData`](https://tanstack.com/query/v5/docs/react/guides/ssr#get-started-fast-with-initialdata).
@@ -452,30 +467,21 @@ In order to use the Devtools, you need to install it additionally.
452
467
$ npm i @tanstack/react-query-devtools --save-dev
453
468
```
454
469
455
-
All you have to do is put the `<ReactQueryDevtools />` within `<QueryClientProvider />`.
470
+
All you have to do is put the `<ReactQueryDevtools />` within `<QueryClientAtomProvider />`.
0 commit comments