Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React set state property dynamically

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){...}

?

like image 496
Anh Tuan Nguyen Avatar asked Jan 05 '23 23:01

Anh Tuan Nguyen


1 Answers

Like this

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

Example

like image 160
Oleksandr T. Avatar answered Jan 10 '23 23:01

Oleksandr T.