title: Handling controlled form component changes in React
body: <p><strong>TLDR:</strong> Common design requirement seems impossible in React without violating React principles or ignoring standard encapsulation opportunities.</p>
  
  <p>I have a form that displays existing data from a server API, and allows the user to edit the fields and update the data on the server. This happens dynamically as the user changes form input values, rather than requiring them to 'submit' the form after each edit.</p>
  
  <p>For some form inputs, the value change is made immediately (e.g. checkbox, radio button, select). For others, the value change is made incrementally - most obviously for the text input. I would not want a server request on every keystroke, as this will lead to server-side validation errors being generated for incomplete values. Instead, the server would be updated once the user leaves the text input field. It would also be a waste to send a server request if the values are unchanged.</p>
  
  <p>In React, the key design principle seems to be that you maintain a single source of truth/state and have this permeate down through components using props. Components should not maintain their own state as a copy of the props, but instead should render the props directly [1]. The 'single source' would be the parent component that pulls and pushes data from and to the server.</p>
  
  <p>For a rendered <code>&lt;input&gt;</code> element to keep its value up to date with server changes, it should use a <code>value</code> attribute, since <code>defaultValue</code> is only evaluated on first render [2]. The React design principles imply I should set <code>&lt;input value={this.props.value} /&gt;</code>. In order for this to respond to user input, an <code>onChange</code> handler must also be provided, bubbling the change up to the parent component, which will update the state and cause the <code>&lt;input&gt;</code> to be re-rendered with an updated <code>props</code>.</p>
  
  <p>However, I would not want to trigger a server request in the <code>onChange</code> handler, since this will trigger on every keystroke. I would need to trigger the server request instead on an <code>onBlur</code> event, supposing that the value has changed since the <code>onFocus</code>. Requiring this for some elements and not others means that the parent component would need two handlers: an <code>onChange</code> handler to update the state for all child components and fire a server request for certain fields, and an <code>onBlur</code> to fire a server request for the other fields. Requiring the parent component to know which of the child form components should exhibit which behaviour seems like a failure to encapsulate properly. The child components should be able to monitor their own values and decide when to emit a 'do something' event.</p>
  
  <p>I cannot see a way to achieve this without violating one of the React principles, most likely by maintaining a state within each form component. Something like this:</p>
  
  <pre><code>class TextInput extends React.Component {
      constructor(props) {
          super(props);
          this.initialValue = this.props.value;
          this.setState({value: this.props.value});
      }
      componentWillReceiveProps = (nextProps) =&gt; {
          this.initialValue = nextProps.value;
          this.setState({value: nextProps.value});
      };
      handleFocus = (e) =&gt; {
          this.initialValue = e.target.value;
      };
      handleChange = (e) =&gt; {
          this.setState({value: e.target.value});
      };
      handleBlur = (e) =&gt; {
          if (e.target.value !== this.initialValue &amp;&amp;
              this.props.handleChange) {
                  this.props.handleChange(e);
          }
      };
      render() {
          return (
              &lt;input type="text"
                     value={this.state.value}
                     onFocus={this.handleFocus}
                     onChange={this.handleChange}
                     onBlur={this.handleBlur} /&gt;
          );
      }
  }
  
  class FormHandler extends React.Component {
      componentDidMount() {
          // fetch from API...
          this.setState(apiResponse);
      }
      handleChange = (e) =&gt; {
          // update API with e.target.value....
      };
      render() {
          return (&lt;TextInput value={this.state.value}
                             handleChange={this.handleChange} /&gt;);
      }
  }
  </code></pre>
  
  <p>Is there a better way of achieving this, without breaking React's principles of trickling props down to be rendered?</p>
  
  <p>Further reading of various attempts to resolve this in [3-4].</p>
  
  <p>Other SO questions from people struggling with a similar problem in [5-6]. </p>
  
  <p>[1] <a href="https://facebook.github.io/react/tips/props-in-getInitialState-as-anti-pattern.html" rel="noreferrer">https://facebook.github.io/react/tips/props-in-getInitialState-as-anti-pattern.html</a></p>
  
  <p>[2] <a href="https://facebook.github.io/react/docs/forms.html#default-value" rel="noreferrer">https://facebook.github.io/react/docs/forms.html#default-value</a></p>
  
  <p>[3] <a href="https://discuss.reactjs.org/t/how-to-pass-in-initial-value-to-form-fields/869" rel="noreferrer">https://discuss.reactjs.org/t/how-to-pass-in-initial-value-to-form-fields/869</a></p>
  
  <p>[4] <a href="https://blog.iansinnott.com/managing-state-and-controlled-form-fields-with-react/" rel="noreferrer">https://blog.iansinnott.com/managing-state-and-controlled-form-fields-with-react/</a></p>
  
  <p>[5] <a href="https://stackoverflow.com/questions/37946229/how-do-i-reset-the-defaultvalue-for-a-react-input/37946963#37946963">How do I reset the defaultValue for a React input</a></p>
  
  <p>[6] <a href="https://stackoverflow.com/questions/30345822/using-an-input-field-with-onblur-and-a-value-from-state-blocks-input-in-reactjs">Using an input field with onBlur and a value from state blocks input in Reactjs JSX?</a></p>