Skip to content
Source
Sign inSign up
TanStack/query

Custom cache key #4

Closedjohn-pc(GitHub profile, opens in a new tab) opened this issue 4 comments

Hey Tanner, thank you for another amazing library.

I would like to suggest to add custom cache key from useQuery, since the query be can changed depending on the implementation, it would be very helpful to be able to control the cache key name

  1. So instead of relying on the idempotency of the query for the cache keying, you want to override the ID it would be given by an arbitrary string?

  2. If that's the case, then we would need to propagate the manual key to this location here: https://github.com/tannerlinsley/react-query/blob/master/src/index.js#L524. It would need to originate in the query options and get passed through a few functions to get there.

  3. tannerlinsleycommented

    Though not fully documented yet. You can now use a custom queryID option to override the generated queryID normally returned from the function, eg.

    // You must memoize the query function. useQuery will still detect changes to the
    // `query` reference and try to refetch if it changes
    const queryFn = React.useCallback(() => axios.get('/todos'), [])
    
    // Then just pass a `queryID` string:
    useQuery(queryFn, { queryID: 'fetchTodos' })
  4. tannerlinsley closed this

Sign in to join this conversation.