React 技巧

setState 处理嵌套数组

参考: React Forms - Controlled Components

方法一: 使用 Object.assign

1
foo: Object.assign({}, this.state.foo, {bar: 1})

方法二: 使用展开运算符

1
2
3
4
foo: {
...this.state.foo,
bar: 1
}

方法三: 使用 update

(使用 immutability-helper 或 immer 等库)