Skip to content

Commit

Permalink
dehydrate react-query
Browse files Browse the repository at this point in the history
Signed-off-by: Nik Nasr <[email protected]>
  • Loading branch information
nikrooz committed Dec 31, 2024
1 parent 58dd813 commit 6c67fcc
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion libs/util/react-query/src/lib/QueryProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@ import {
QueryClient,
QueryClientProvider,
QueryCache,
hydrate,
} from '@tanstack/react-query';
import { PropsWithChildren, useState } from 'react';
import { useMatches } from 'react-router';

const useDehydratedState = (queryClient: QueryClient) => {
const matches = useMatches();

matches
.map((match) => (match.data as any)?.dehydratedState)
.filter(Boolean)
.forEach((dehydratedState) => {
hydrate(queryClient, dehydratedState);
});
};

export function QueryProvider({
children,
Expand Down Expand Up @@ -33,7 +46,7 @@ export function QueryProvider({
},
})
);

useDehydratedState(queryClient);
return (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
Expand Down

0 comments on commit 6c67fcc

Please sign in to comment.