pmndrs / zustand

🐻 Bear necessities for state management in React
https://zustand-demo.pmnd.rs/
MIT License
48.35k stars 1.5k forks source link

recommend way to deep merge state inside set function #551

Closed lili21 closed 3 years ago

lili21 commented 3 years ago
import create from 'zustand';

const useStore = create(set => ({
  test: { name: 'lili', age: 10 },
  update: () => set({ test: { age: 11 }})
})

the expected result state is test: { name: 'lili', age: 11}

dai-shi commented 3 years ago

quick answer:

  1. merge it on your own: set(state => ({ test: { ...state.test, age: 11 } }))
  2. use immer produce or middleware
  3. (prefer valtio 🤫 )