qqxu
3/12/2018 - 9:06 AM

dynamic change state name and state value

dynamic change state name and state value

  1. set varible state
<Select onChange={val => this.handleChange(val, 'type')}>
  <Option value="1">approver</Option>
  <Option value="2">acceptor</Option>
 </Select>

// write
handleChange = (val, string) => {
    this.setState({
      [string]: val,   // Equivalent to  type: val, 
    });
 };
 
  1. read state
// read
  let a = 'approving';
  console.log(this.state[`${a}Tips`]);