Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 使用新的任意对象更新嵌套的反应状态对象_Reactjs - Fatal编程技术网

Reactjs 使用新的任意对象更新嵌套的反应状态对象

Reactjs 使用新的任意对象更新嵌套的反应状态对象,reactjs,Reactjs,当react state对象中的值是对象时,如何更新该值?例如: this.state = { nestedObj: { x: 0, y: 5 } }; 稍后,我想使用基于用户输入的JSON.parse创建的对象的任意对象更新该nestedObj 我尝试了以下方法,但不起作用: const newObj = {nestedObj: { x: 0, arbitraryKey: 'bla', anotherOne: { h: 0 }}}; this.setState(newObj); 我真的很想

当react state对象中的值是对象时,如何更新该值?例如:

this.state = { nestedObj: { x: 0, y: 5 } };
稍后,我想使用基于用户输入的
JSON.parse
创建的对象的任意对象更新该nestedObj

我尝试了以下方法,但不起作用:

const newObj = {nestedObj: { x: 0, arbitraryKey: 'bla', anotherOne: { h: 0 }}};
this.setState(newObj);
我真的很想把驻留在
this.state.nestedObj的任何对象吹走,并用
newObj
中定义的任何对象替换它。我该怎么做?我的
this.state中有其他键,因此如果这只影响
nestedObj
就很理想了,但我不是特别挑剔。 谢谢

类主扩展React.Component{
建造师(道具){
超级(道具);
此.state={
obj:{a:你好,b:世界}
};
}
componentDidMount(){
log('obj被初始化为:',this.state.obj);
}
handleClick=()=>{
常量值=Math.random().toFixed(2);
这是我的国家({
对象:{a:value,b:value}
},()=>console.log('obj改为:',this.state.obj));
};
render(){
返回按钮
}
}
ReactDOM.render(
, 
document.getElementById('root'))
);

您是否尝试过
此.setState({nestedObj:newObj})