Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/391.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
Javascript 如何在reactJS状态下动态设置对象属性的值?_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 如何在reactJS状态下动态设置对象属性的值?

Javascript 如何在reactJS状态下动态设置对象属性的值?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,假设组件具有以下状态: this.state = { enabled: { one: false, two: false, three: false } } 如何使用this.setState设置动态属性的值 例如,这不起作用: let dynamicProperty = "one" this.setState({ enabled[dynamicProperty]: true }) 然而,这确实有效,但也是一种不好的做法: this.enabled = {

假设组件具有以下状态:

this.state = {
  enabled: {
    one: false,
    two: false,
    three: false
  }
}
如何使用this.setState设置动态属性的值

例如,这不起作用:

let dynamicProperty = "one"
this.setState({
  enabled[dynamicProperty]: true
})
然而,这确实有效,但也是一种不好的做法:

this.enabled = {
  one: false,
  two: false,
  three: false
}
let dynamicProperty = "one"
this.enabled[dynamicProperty] = true;

如何使用this.setState完成相同的任务?

您需要创建原始对象的副本,并且只更改要更新的属性。最简单的方法是使用“对象扩展”操作符:

this.setState(currentState => ({enabled: {...currentState.enabled, one: true}}));
或者以更详细的形式:

this.setState(currentState => {
    const enabled = {...currentState.enabled, one: true};
    return {enabled};
});
如果仅在运行时才知道属性名称,则可以这样做:

const setEnabled = name => {
    this.setState(currentState => ({enabled: {...currentState.enabled, [name]: true}}));
};

您需要创建原始对象的副本,并且只更改要更新的属性。最简单的方法是使用“对象扩展”操作符:

this.setState(currentState => ({enabled: {...currentState.enabled, one: true}}));
或者以更详细的形式:

this.setState(currentState => {
    const enabled = {...currentState.enabled, one: true};
    return {enabled};
});
如果仅在运行时才知道属性名称,则可以这样做:

const setEnabled = name => {
    this.setState(currentState => ({enabled: {...currentState.enabled, [name]: true}}));
};

标准做法是复制状态,修改复制的状态,然后使用该克隆设置状态,如下所示:

//with spread operator
const enabledClone = {...this.state.enabled};
enabledClone.one = true;
this.setState({enabled : enabledClone});

标准做法是复制状态,修改复制的状态,然后使用该克隆设置状态,如下所示:

//with spread operator
const enabledClone = {...this.state.enabled};
enabledClone.one = true;
this.setState({enabled : enabledClone});

可以在对象的键周围使用大括号来使用变量来确定键

const dynamicKey = 'one';
const newObj = {[dynamicKey]: true} //equals {one: true}
由于this.setState仅在顶级键上合并,因此必须创建当前启用对象的副本并使用大括号表示法:

 let dynamicProperty = "one"
 this.setState({
   enabled: {...this.state.enabled, [dynamicProperty]: true}
 })   

可以在对象的键周围使用大括号来使用变量来确定键

const dynamicKey = 'one';
const newObj = {[dynamicKey]: true} //equals {one: true}
由于this.setState仅在顶级键上合并,因此必须创建当前启用对象的副本并使用大括号表示法:

 let dynamicProperty = "one"
 this.setState({
   enabled: {...this.state.enabled, [dynamicProperty]: true}
 })   

嗯,由于某种原因,这对我不起作用编辑:对[propertyName]的更改对我不起作用编辑:对[propertyName]的更改对我起作用,但是我对@trxn给出了正确的答案,因为我回答了他答案的前2部分A更简洁这对我起作用,然而,我对@trxn给出了正确的答案,因为我对他答案的前2部分A的回答更为简洁