4

I'm using react and I have some methods to set the state of my COmponent separately. I have the following methods:

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());
 });
}

How can I combine the methods, so that I can have something like:

setAttribute(propery,value){...}

?

Anh Tuan Nguyen
  • 971
  • 3
  • 13
  • 28
  • Possible duplicate of [React set state property and its value dynamically](https://stackoverflow.com/questions/45862136/react-set-state-property-and-its-value-dynamically) – buratino Jul 12 '19 at 17:10

1 Answers1

9

Like this

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

Example

Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144