Skip to content

Commit

Permalink
docs: Update query-key-factory example (#5277)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemorales authored Apr 15, 2023
1 parent a9ddb4d commit b99ae60
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions docs/react/community/lukemorales-query-key-factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ import { createQueryKeyStore } from '@lukemorales/query-key-factory'
export const queryKeys = createQueryKeyStore({
users: null,
todos: {
completed: null,
search: (query: string, limit = 15) => [query, limit],
byId: (todoId: string) => ({ todoId }),
detail: (todoId: string) => [todoId],
list: (filters: TodoFilters) => ({
queryKey: [{ filters }],
queryFn: (ctx) => api.getTodos({ filters, page: ctx.pageParam }),
}),
},
})
```
Expand All @@ -44,9 +46,11 @@ export const usersKeys = createQueryKeys('users')

// my-api/todos.ts
export const todosKeys = createQueryKeys('todos', {
completed: null,
search: (query: string, limit = 15) => [query, limit],
byId: (todoId: string) => ({ todoId }),
detail: (todoId: string) => [todoId],
list: (filters: TodoFilters) => ({
queryKey: [{ filters }],
queryFn: (ctx) => api.getTodos({ filters, page: ctx.pageParam }),
}),
})

// my-api/index.ts
Expand All @@ -60,13 +64,13 @@ import { queryKeys, completeTodo, fetchSingleTodo } from '../my-api'
export function Todo({ todoId }) {
const queryClient = useQueryClient()

const query = useQuery({ queryKey: queryKeys.todos.byId(todoId), queryFn: fetchSingleTodo })
const query = useQuery(queryKeys.todos.detail(todoId))

const mutation = useMutation({
mutationFn: () => completeTodo,
onSuccess: () => {
// Invalidate and refetch
queryClient.invalidateQueries({ queryKey: queryKeys.todos.completed })
queryClient.invalidateQueries({ queryKey: queryKeys.todos.list.queryKey })
},
})

Expand Down

0 comments on commit b99ae60

Please sign in to comment.