React
  • Introduction
  • Getting Started
    • Introduction
    • Before you get started
  • 1. Fundamentals
    • Introduction
    • Rendering
      • JSX
      • Exercise
      • Solution
    • Components
      • Stateless
        • Exercise
        • Solution
      • Stateful
        • Exercise
        • Solution
      • Styling
        • Exercise
        • Solution
        • Using CSS Modules
    • Folder Architecture
  • 2. Intermediate
    • Lifecycle methods
    • Controlled and Uncontrolled components
    • Anti-patterns
    • Refs and the DOM
    • Lifting State Up
  • 3. Advanced Topics
    • Conventions
    • Reconciliation
    • Performance Optimizations
      • Avoiding Reconciliation
      • PureComponent
      • Avoiding inline lambdas
      • Development vs Production build
    • Context
  • 4. Advanced Patterns
    • Higher-order Components
    • Children as Function
    • Renderless Components
    • Portals
    • Error handling
  • Exercises
    • Introduction
    • 1. ProductList light
      • Step 1
      • Step 2
      • Step 3
      • Step 4
      • Extra
      • Solution
Powered by GitBook
On this page
  • References
  • Uncontrolled components
  • Note:
  • Controlled components
  1. 2. Intermediate

Controlled and Uncontrolled components

PreviousLifecycle methodsNextAnti-patterns

Last updated 6 years ago

References

Uncontrolled Components – React Official docs

Uncontrolled components

An uncontrolled component keeps the source of truth in the DOM. A good example for this, are inputs.

Take this example for instance:

class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleSubmit(event) {
    alert('A name was submitted: ' + this.input.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input type="text" ref={(input) => this.input = input} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

Here, the code relies of the value of the input in the DOM rather than in its component state.

Note:

In most cases, it's recommend to use controlled components to implement forms.

Controlled components

In a controlled component, the data are handled by the React component and is usually stored in its state.

Here is the same above example with a controlled component:

class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert('A name was submitted: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input type="text" value={this.state.value} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

Take the time to look at the other example of the React official documentation:

  • Textarea tag

  • Select tag

  • Handling multiple inputs

Controlled Components – React Official docs

Controlled and uncontrolled form inputs in React don't have to be complicated

https://reactjs.org/docs/forms.html#controlled-components
https://goshakkk.name/controlled-vs-uncontrolled-inputs-react/
https://reactjs.org/docs/forms.html#the-textarea-tag
https://reactjs.org/docs/forms.html#the-select-tag
https://reactjs.org/docs/forms.html#handling-multiple-inputs
https://reactjs.org/docs/uncontrolled-components.html