State Management · Senior · Question 57 of 75

How do you give each instance of a complex widget its own Zustand store?

const StoreContext = createContext(null);
function Editor({ initial }) {
  const [store] = useState(() => createStore(() => ({ ...initial })));
  return <StoreContext value={store}>...</StoreContext>;
}

Explanation

This also allows initializing from props. Creating the store in a useState initializer keeps it stable across renders.

Further reading

Problem with a question

State Management · Senior · question 57

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