Next.js with initialData + pagination #2678
-
So I have been doing some tests with I've followed this example on the docs: https://react-query.tanstack.com/guides/ssr#using-initialdata, basically I fetch the data using export async function getServerSideProps() {
const data = await fetch(/*content from page 1*/)
return {
props: {
initialData: data,
},
}
}
function Component() {
const Home = (props) => {
const [page, setPage] = useState(1)
const { data } = useQuery(
['list', page],
() => fetch(...)
{
initialData: props.initialData,
}
)
} The problem here is that the const [page, setPage] = useState(1)
const { data } = useQuery(
['list', page],
() => fetch(...)
{
initialData: page === 1 ? props.initialData : undefined,
}
) I only set the initialData if the page is 1, if it's not, then nothing is set. This works just fine, the first page is loaded up with the data from |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Just found that an issue was created for this situation before: #78 We had a few solutions in there for this scenario, a few using |
Beta Was this translation helpful? Give feedback.
-
that's totally legit. The only more generic solution would be to use with that, you would use |
Beta Was this translation helpful? Give feedback.
that's totally legit. The only more generic solution would be to use
hydration
: https://react-query.tanstack.com/guides/ssr#using-hydrationwith that, you would use
await queryClient.prefetchQuery(['list', 1], () => fetch(/*content from page 1*/))
and then send the whole dehydrated QueryClient to the frontend, where you would hydrate it. Once you get around to youruseQuery
where you want data for the first page, you should i…