const queryClient = useQueryClient();
const updateWord = trpc.word.update.useMutation({
onMutate: async newTodo => {
// Cancel any outgoing refetches (so they don't overwrite our optimistic update)
await queryClient.cancelQueries({ queryKey: ['text', 'getOne'] })
// Snapshot the previous value
const previousText = queryClient.getQueryData(['text', 'getOne'])
// Optimistically update to the new value
queryClient.setQueryData(['text', 'getOne'], old => old ? {...old, { title: "Hello" }} : undefined)
// Return a context object with the snapshotted value
return { previousText }
},
//...
const queryClient = useQueryClient();
const updateWord = trpc.word.update.useMutation({
onMutate: async newTodo => {
// Cancel any outgoing refetches (so they don't overwrite our optimistic update)
await queryClient.cancelQueries({ queryKey: ['text', 'getOne'] })
// Snapshot the previous value
const previousText = queryClient.getQueryData(['text', 'getOne'])
// Optimistically update to the new value
queryClient.setQueryData(['text', 'getOne'], old => old ? {...old, { title: "Hello" }} : undefined)
// Return a context object with the snapshotted value
return { previousText }
},
//...