Skip to content

Ability to persist only specific queries #8323

Answered by TkDodo
b-ponomarenko asked this question in Q&A
Discussion options

You must be logged in to vote

step 1: add the meta option to the queries that you want to persist:

const query = useQuery({
  queryKey: 'data',
  queryFn: () => fetch('api'),
  meta: {
    persist: true
  }
})

step 2: filter on the PersistQueryClientProvider what you want to persist:

<PersistQueryClientProvider
  client={queryClient}
  persistOptions={{
    persister,
    dehydrateOptions: {
      shouldDehydrateQuery: (query) => defaultShouldDehydrateQuery(query) && query.meta.persist === true,
    },
  }}
>

defaultShouldDehydrateQuery can be imported and makes sure that only successful queries are dehydrated.

you can also invert it and per default, persist everything and then opt-out when persist is set to false, …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@b-ponomarenko
Comment options

Answer selected by b-ponomarenko
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants