-
I'm using react-router-dom, react-query, react-query-persist-client, and query-sync-storage-persister. I set it up based on the docs. I have a default Here's a repro: If you open the console, you'll see a "fetching" log every time you refresh the tab. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 9 replies
-
I'm pretty sure the Best thing you can do is probably build a
|
Beta Was this translation helpful? Give feedback.
-
@TkDodo thanks for the response. Unfortunately that doesn't work. I updated the sandbox: https://codesandbox.io/p/sandbox/eager-lake-cq952g?embed=1&file=/src/index.jsx:6,23 |
Beta Was this translation helpful? Give feedback.
-
ok this is what i'm going with: import React from "react"
import ReactDOM from "react-dom/client"
import { persistQueryClient } from "@tanstack/react-query-persist-client"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import { createHashRouter, RouterProvider } from "react-router-dom"
import { createSyncStoragePersister } from "@tanstack/query-sync-storage-persister"
import { Root } from "~/Root"
const queryClient = new QueryClient({
defaultOptions: {
queries: {
cacheTime: 1000 * 60 * 60 * 24, // 24 hours
staleTime: 1000 * 60 * 5, // 5 minutes
},
},
})
void (async function main() {
// await the promise that lets us know the cache hydration is complete
await persistQueryClient({
queryClient,
persister: createSyncStoragePersister({ storage: window.localStorage }),
})[1]
// create the router *after* cache is hydrated so loaders occur after
const router = createHashRouter([
{
path: "/",
element: <Root />,
id: "root",
loader: async () => {
return await queryClient.ensureQueryData({
queryKey: ["data"],
queryFn: async () => {
const res = await fetch("https://domain.com/api/data")
return await res.json()
},
})
},
},
])
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
</QueryClientProvider>
</React.StrictMode>
)
})() The The docs say this about using
but i looked at the code and the only |
Beta Was this translation helpful? Give feedback.
-
@dadamssg I think this is the solution I would prefer: https://codesandbox.io/p/sandbox/vigorous-wildflower-7khqlw?embed=1&file=/src/index.jsx:59,2 The idea is to create the router inside the react component (in state, not memo), but in a child component that will only be rendered below the |
Beta Was this translation helpful? Give feedback.
-
@TkDodo interesting. I had not read that article before. I wouldn't have thought my router instance would be an appropriate state value. I've always been under the impression that you shouldn't keep rendered elements in state. That state is only for serializable data. TIL. |
Beta Was this translation helpful? Give feedback.
-
i am also very confused with the persist mechanism, perhaps the doc is not so explicit to me. my problems:
|
Beta Was this translation helpful? Give feedback.
@dadamssg I think this is the solution I would prefer:
https://codesandbox.io/p/sandbox/vigorous-wildflower-7khqlw?embed=1&file=/src/index.jsx:59,2
The idea is to create the router inside the react component (in state, not memo), but in a child component that will only be rendered below the
PersistGate
.