State Management · Senior · Question 69 of 75
How do you update an edited todo in every cached list (filtered views) at once?
queryClient.setQueriesData({ queryKey: ["todos", "list"] }, (old) =>
old?.map((t) => (t.id === updated.id ? updated : t)));- ✓Correct answer: Use
setQueriesDatawith a key prefix, or simply invalidate the prefix - 2Call
setQueryDataonce with the shortest key - 3Lists can't be updated, only refetched one by one
Explanation
setQueriesData applies the updater to every matching query. Invalidating is simpler when list membership might change (e.g. completed todos leave a filter).