Created
          July 14, 2021 08:54 
        
      - 
      
- 
        Save wobsoriano/9f27951e0f64c02e8b4fa09875b0c9be to your computer and use it in GitHub Desktop. 
    react-query Query Key Factory
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | export const todoKeys = { | |
| all: ['todos'] as const, | |
| lists: () => [...todoKeys.all, 'list'] as const, | |
| list: (filters: string) => [...todoKeys.lists(), { filters }] as const, | |
| details: () => [...todoKeys.all, 'detail'] as const, | |
| detail: (id: number) => [...todoKeys.details(), id] as const, | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // ๐บ remove everything related to the todos feature | |
| queryClient.removeQueries(todoKeys.all) | |
| // ๐ invalidate all lists | |
| queryClient.invalidateQueries(todoKeys.lists()) | |
| // ๐ prefetch a single todo | |
| queryClient.prefetchQueries(todoKeys.detail(id), () => fetchTodo(id)) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
From: https://tkdodo.eu/blog/effective-react-query-keys