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)));

Explanation

setQueriesData applies the updater to every matching query. Invalidating is simpler when list membership might change (e.g. completed todos leave a filter).

Further reading

Problem with a question

State Management · Senior · question 69

Test yourself

Take the State Management · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz