Skip to content

Instantly share code, notes, and snippets.

@wobsoriano
Created July 14, 2021 08:54
Show Gist options
  • Save wobsoriano/9f27951e0f64c02e8b4fa09875b0c9be to your computer and use it in GitHub Desktop.
Save wobsoriano/9f27951e0f64c02e8b4fa09875b0c9be to your computer and use it in GitHub Desktop.
react-query Query Key Factory
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,
}
// ๐Ÿ•บ 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))
@wobsoriano
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment