T
TanStack4y ago
rare-sapphire

Can we keep previous data longer..?

I'm building a pagination feature using react-query. keepPreviousData is an amazing feature to me, but it's not enough for this time. I want to keep old data until I finish making some changes right after API fetching/refetching. for example..
useQuery(['key', parentId], fetcher, {
onSuccess: () => {
// I want to keep previous data until I finish onSuccess callback.
},
keepPreviousData: true,
}
useQuery(['key', parentId], fetcher, {
onSuccess: () => {
// I want to keep previous data until I finish onSuccess callback.
},
keepPreviousData: true,
}
Here's what I tried: I created new state to hold old data.
const [keepData, setKeepData] = useState();
useQuery(['key', ...], fetcher, {
onSuccess: (data) => {
// ...
setKeepData(data)
}
}
const [keepData, setKeepData] = useState();
useQuery(['key', ...], fetcher, {
onSuccess: (data) => {
// ...
setKeepData(data)
}
}
but.. could anyone share some workarounds to do this using react-query only?
2 Replies
ambitious-aqua
ambitious-aqua4y ago
Whatever you are doing in onSuccess should likely be done directly in the queryFn
rare-sapphire
rare-sapphireOP4y ago
Oh! Thx a lot!

Did you find this page helpful?