我想在React中编写一个函数。 在我的类中,我有一个状态对象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
?