Skip to content

How to wait for state changes before doing an refetch. #5720

Answered by TkDodo
PazminoJose asked this question in Q&A

You must be logged in to vote

enabled:false + refetch opts out of what react-query wants to do, so I can't help much here. Calling clearFilter and then refetch doesn't work because when you call refetch, your queryFunction still sees the closure with the old filters. It's like calling setState and then expecting the state to be updated, synchronously. That's just not how react works.

The recommended solution is to:

  • add the filters to the QueryKey
  • enable / disable the query conditionally

pretty much what the lazy query example in the docs is doing: https://tanstack.com/query/v4/docs/react/guides/disabling-queries#lazy-queries

Replies: 1 comment 1 reply

You must be logged in to vote
1 reply
@dberardo-com

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