Using the hydrate pattern
With the hydrate
pattern, you can dehydrate your QueryClient
with a previously prefetched query and send it to your client. On the client side, as soon as the page loads and JavaScript is available, React Query will hydrate your QueryClient
with the existing data. After this process, React Query will also ensure your queries are up to date.
This is the process of how to leverage the best of your server-side framework and React Query with the hydrate
pattern:
- The first thing you do is create a
QueryClient
instance. - Using the previously created
QueryClient
instance, you leverage itsprefetchQuery
method to prefetch the data for that given query key. - You dehydrate your
QueryClient
and send it to the client side. - Your client receives the dehydrated state, hydrates it, and merges it with the
QueryClient
in use. - Inside your component, you render your query using the
useQuery
hook with the same query key you added in step 2. Your query...