-
I'm using Why does the client call What am I missing here?
const Home: NextPage = () => {
const helloInfo = useHello();
return <h1>Hello {helloInfo.data?.name}</h1>;
};
export const getServerSideProps: GetServerSideProps = async () => {
const queryClient = new QueryClient();
await queryClient.prefetchQuery("hello", fetchHello);
return {
props: {
dehydratedState: dehydrate(queryClient),
},
};
};
export const fetchHello = async (): Promise<HelloResponse> => {
console.log("fetchHello is called");
return new Promise((resolve) => {
setTimeout(() => {
resolve({
name: "John Doe",
});
}, 1000);
});
};
export const useHello = () => {
return useQuery<HelloResponse>("hello", fetchHello);
};
function MyApp({ Component, pageProps }: AppProps) {
const [queryClient] = useState(() => new QueryClient());
return (
<QueryClientProvider client={queryClient}>
<Hydrate state={pageProps.dehydratedState}>
<Component {...pageProps} />
</Hydrate>
</QueryClientProvider>
);
} Full source code is available here: https://github.com/moroshko/react-query-hydration |
Beta Was this translation helpful? Give feedback.
Answered by
TkDodo
Mar 16, 2022
Replies: 1 comment
-
it does use the hydrated name, but it gives you a background refetch because staleTime defaults to zero. That is documented here: https://react-query.tanstack.com/guides/ssr#staleness-is-measured-from-when-the-query-was-fetched-on-the-server |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
moroshko
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it does use the hydrated name, but it gives you a background refetch because staleTime defaults to zero. That is documented here: https://react-query.tanstack.com/guides/ssr#staleness-is-measured-from-when-the-query-was-fetched-on-the-server