动态设置状态属性

Anh*_*yen 2 state javascript-objects reactjs

我正在使用react,我有一些方法来分别设置我的COmponent的状态.我有以下方法:

setLineColor(value){
  this.setState({stroke:value},()=>{
  this.props.data(this.getStyleData());
 });
}
setFillColor(value){
 this.setState({ fill:value},()=>{
 this.props.data(this.getStyleData());
 });
}
setMode(value){
 this.setState({ mode:value},()=>{
 this.props.data(this.getStyleData());
 });
}
Run Code Online (Sandbox Code Playgroud)

我如何组合这些方法,以便我可以有类似的东西:

setAttribute(propery,value){...}
Run Code Online (Sandbox Code Playgroud)

Ale*_* T. 6

像这样

setAttribute(property, value) { 
  this.setState({ [property]: value }, () => {
    this.props.data(this.getStyleData());
  });
}
Run Code Online (Sandbox Code Playgroud)

Example