getInitialState: function() { return { x: 0, y: 0 };}
您可以用来
setState在该对象上设置单个键:
this.setState({ x: 1 }); // y still == 0
React不会智能合并您的状态;例如,这不起作用:
getInitialState: function() { return { point: { x: 0, y: 0 }, radius: 10 };}this.setState({point: {x: 1}});// state is now == {point: {x: 1}, radius: 10} (point.y is gone)
[编辑]
如@ssorallen所述,您可以使用不变性帮助器来获得想要的效果:
var newState = React.addons.update(this.state, { point: { x: {$set: 10} }});this.setState(newState);
- 有关示例,请参见此JSFiddle:http
- //jsfiddle.net/BinaryMuse/HW6w5/
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)