Skip to content

Allow refetch() in useQuery to Accept Query Parameters #8662

Answered by TkDodo
dimassibassem asked this question in General
Discussion options

You must be logged in to vote

usually, everything that is used inside the queryFn should be part of the queryKey. it sounds like forceRefresh is an exception because you don’t actually want to store the result in a different key?

passing a different queryFn is always dangerous because the queryFn gets stored on the query and automatic refetches might re-use those, so I would keep the same function.

I would probably create a ref and pass that through meta:

const forceRefresh = useRef(false)

const { refetch } = useQuery({
  queryKey,
  queryFn: ({ meta }) => fetchUsers({ forceRefresh: meta.forceRefresh.current }),
  meta: {
    forceRefresh
  }
})

const handleRefresh = () => {
  forceRefresh.current = true
  refetch()…

Replies: 1 comment 6 replies

Comment options

You must be logged in to vote
6 replies
@TkDodo
Comment options

@dimassibassem
Comment options

@dimassibassem
Comment options

@TkDodo
Comment options

Answer selected by dimassibassem
@dimassibassem
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants