核心实现是利用 ES6 的 proxy,几乎以最小的成本实现了 js 的不可变数据结构
state: {
name: 'ronffy',
age: 30
}
// 用immer更新state
import produce from "immer";
//代码量更少,也较清晰
this.setState(produce(draft => {
draft.age++;
}))
// 常规的操作
this.setState(prevState => ({
age: prevState.age + 1
}))
// immer优化reducer
const reducer = (state, action) => produce(state, draft => {
draft.age++;
})
const reducer = produce((draft, action) => {
draft.age++;
})