我不知道这是否是正确的解决方案,以更新状态有两个dictionares阵营JS的setState({字典:字典})
var PopulationCityView = React.createClass({
getInitialState: function() {
return {
prod_diff : {'wheat':0,'meat':0,'fish':0,'bread':0,'fruit':0,'wine':0,'beer':0,'wool':0,'cloth':0,'leather':0,'paper':0,'ceramics':0,'furniture':0,'glass':0}
};
},
componentWillMount: function() {
this.prod_diff = {'wheat':0,'meat':0,'fish':0,'bread':0,'fruit':0,'wine':0,'beer':0,'wool':0,'cloth':0,'leather':0,'paper':0,'ceramics':0,'furniture':0,'glass':0};
},
handleM: function(res,child_new_res_diff){
var new_prod_diff = this.prod_diff;
new_prod_diff[res] = child_new_res_diff;
this.setState({prod_diff:new_prod_diff});
},
render: function(){
........
,如果任何人更好,更快的解决方案都知道会要求一个提示。 ..
不要直接修改'this.state'。您可以轻松地将嵌套对象传递给'setState',而不必担心通过调用'forceUpdate'来攻击API。 – couchand
@couchand这就是为什么我建议第一个解决方案更好,但总是有一些边缘案例。然而,你是正确的forceUpdate是危险的,所以我纠正了我的第二个解决方案。 – daniula