State Management · Junior · Question 37 of 75
How do you save a Zustand store to localStorage?
- ✓Correct answer: Wrap the store creator in the
persistmiddleware - 2Zustand saves every store to
localStorageautomatically - 3Call
localStorage.setIteminside each component after rendering
Explanation
create(persist((set) => ({...}), { name: 'cart' })) saves and restores the state. You can choose the storage, persist only some fields (partialize) and migrate old versions.