Skip to content

How to dynamically swap queryClient and persister based on user action (multiple user accounts use-case)? #8099

Closed Answered by nop33
nop33 asked this question in Q&A
Discussion options

You must be logged in to vote

This is how I ended up solving this.

// Inspired by https://github.com/TanStack/query/blob/1adaf3ff86fa2bf720dbc958714c60553c4aae08/packages/react-query-persist-client/src/PersistQueryClientProvider.tsx
export const PersistQueryClientContextProvider = ({ children }: { children: ReactNode }) => {
  const [isRestoring, setIsRestoring] = useState(true)
  const [unsubscribeFromQueryClientFn, setUnsubscribeFromQueryClientFn] = useState(() => () => {})

  const clearQueryCache = useCallback(() => {
    unsubscribeFromQueryClientFn()

    queryClient.clear()
  }, [unsubscribeFromQueryClientFn])

  const restoreQueryCache = useCallback(async (walletId: string) => {
    setIsRestoring(true)

    c…

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
1 reply
@nop33
Comment options

Comment options

You must be logged in to vote
2 replies
@nop33
Comment options

@nop33
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by nop33
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