Posts produce方法
Post
Cancel

produce方法

参数 (currentState, draftState, patchListener)

作用: 解决引用类型对象被修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface Patch {
  		op: "replace" | "remove" | "add" // 一次更改的动作类型
 		path: (string | number)[] // 此属性指从树根到被更改树杈的路径
  		value?: any // op为 replace、add 时,才有此属性,表示新的赋值
}
const newState = produce(currentState, draftState => {
		// change draftState
}, (patches: Patch[], inversePatches: Patch[]) => {
        // patches 正向操作记录, 修改值之后的数组(draftState)
		// inversePatches 反向操作记录, 修改值之前的数组(currentState)
})
* currenState 初始对象的值
* draftState  被修改后的对象的草稿,不会修改currentState
* newState    返回draftState的值
This post is licensed under CC BY 4.0 by the author.