State Management · Middle · Question 23 of 75

How does set in Zustand handle nested objects?

set({ user: { name: "Ann" } }); // state was { user: { name, email }, theme }

Explanation

set does a shallow merge of the top level. For nested updates spread explicitly: set((s) => ({ user: { ...s.user, name: 'Ann' } })), or use the immer middleware.

Further reading

Problem with a question

State Management · Middle · question 23

Test yourself

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

Take this quiz