我有一个函数要在RESPECT中编写.在我的类中,我有一个状态对象fields
,它看起来像这样:
this.state = {
step: 1,
fields: {
type: '',
name: '',
subtype: '',
team: '',
agreement: ''
}
};
我有各种各样的功能,用immutability helper
来分配这些键,通常看起来像:
assignType(attribute) {
var temp = update(this.state.fields, {
type: {$set: attribute}
});
this.setState({
fields: temp
});
}
我想要做的是使用一个更通用的函数,并执行如下操作:
assignAttribute(field, attribute) {
var temp = update(this.state.fields, {
field: {$set: attribute}
});
this.setState({
fields: temp
});
}
但是,这不管用.使用immutability-helper
使用变量键可以做什么?