【发布时间】:2020-10-31 17:05:07
【问题描述】:
我有一个状态
constructor(props) {
super(props);
this.state = {
qualification: {
fieldOfStudy: "Architecture",
degree: "",
}
};
}
我想在函数调用时动态更新状态值
onChange={this.handleChangeObj("qualification")}
这是我写的函数:
handleChangeObj = (ObjName) => ({ target }) => {
//String Value - Qualification
console.log("handleChangeObj -> ObjName", ObjName);
//fieldOfStudy: Architecture
console.log((target.name + ":" + target.value));
// Doubt over here how to update the value for qualification.fieldOfStudy : Architecture
this.setState(() => ({
...this.state.ObjName,
[target.name]: [target.value],
}));
// this is not working.
this.setState({ [ObjName.target.name]: target.value });
};
【问题讨论】:
标签: javascript reactjs object setstate