State Management · Junior · Question 60 of 75

How does a Zustand action read the current state?

const useCart = create((set, get) => ({
  items: [],
  total: () => get().items.reduce((s, i) => s + i.price, 0),
}));

Explanation

set updates state and get reads the latest snapshot inside actions. Components should read state through selectors to subscribe to changes.

Further reading

Problem with a question

State Management · Junior · question 60

Test yourself

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

Take this quiz