3

In an example of the React docs page Forms, ES6 computed property syntax is used in a method to set the state of the name property.

handleInputChange(event) {
    const target = event.target;
    const value = target.type === 'checkbox' ? target.checked : target.value;
    const name = target.name;

    this.setState({
      [name]: value
    });
  }

Based on my reading of how the computed property works, it seems like the reason it’s used is so target.name can be changed — is that so? If that’s the case it seems like it would just be easier to change it there in setState rather than changing the value of the name variable.

I’m new to React and struggling to understand how the computed property syntax is applied in this example. Any help would be greatly appreciated.