State Management · Senior · Question 55 of 75

What is wrong with this cache update?

queryClient.setQueryData(["todos"], (old) => {
  old.push(newTodo);
  return old;
});

Explanation

Write (old) => old ? [...old, newTodo] : [newTodo]. Cached data is treated as immutable, just like React state.

Further reading

Problem with a question

State Management · Senior · question 55

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