title: React using method parameter as an object key to update state
body: <p>How can I use a function parameter as a property name to update state i.e.</p>
  
  <pre><code>onStudentPinChange = (propertyName, e) =&gt; {
      this.setState({
        formFields: { ...this.state.formFields, propertyName: e.target.value }
      });
  };
  </code></pre>
  
  <p>the code above throw error </p>
  
  <p>I would like to use that <code>propertyName</code> variable as an object key I googled and tried many different techniques like <code>this.state.formFields[propertName]</code> </p>
  
  <p>I am creating a new web-app I have a few form fields which needed to be filled to login to the admin area. I have three methods which are basically doing the same thing just updating the relevant state</p>
  
  <pre><code>onStudentPinChange = (propertyName, e) =&gt; {
      var item = { ...this.state.formFields };
      item[propertyName] = e.target.value;
      console.log(item);
      this.setState({
        formFields: { ...this.state.formFields, studentPin: e.target.value }
      });
    };
  
    onCNICChange = (propertyName, e) =&gt; {
      this.setState({
        formFields: { ...this.state.formFields, cnic: e.target.value }
      });
    };
  
    onPasswordChange = (propertyName, e) =&gt; {
      this.setState({
        formFields: { ...this.state.formFields, password: e.target.value }
      });
    };
  
  
  
  &lt;InputField
          type="text"
          name="student_pin"
          value={this.state.formFields.studentPin}
          placeholder="Student Pin"
          onChange={this.onStudentPinChange.bind(this, "studentPin")}
          faClassName="fa-user"
        /&gt;
  </code></pre>
  
  <p>I would like only to have one function that updates all three fields just using that properName </p>
  
  <p>Regards</p>