Skip to content

Custom cache key #4

Closed
Closed

Description

@john-pc

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

Activity

  1. tannerlinsley commented on Sep 17, 2019

    @tannerlinsley
    Member

    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. tannerlinsley commented on Sep 17, 2019

    @tannerlinsley
    Member

    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. tannerlinsley commented on Sep 23, 2019

    @tannerlinsley
    Member

    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. sithuhub commented on Nov 12, 2019

    @sithuhub
  5. added a commit that references this issue on Jun 5, 2021
  6. added a commit that references this issue on Sep 9, 2022
  7. added a commit that references this issue on Sep 26, 2025
  8. added a commit that references this issue on Feb 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions