react forms

Understanding React Forms

As a developer, I have had the opportunity to work with React Forms a lot. React Forms are an essential part of building any web application that requires user input. They allow you to gather information from users and use it to create dynamic experiences.

Controlled Components

One of the ways to work with React Forms is through Controlled Components. Controlled Components are components that render form elements and control them by keeping the form data in the component's state.


import React, { Component } from 'react';

class App extends 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>
    );
  }
}

export default App;

This code creates a simple form with a single input field for a name. The value of the input field is stored in the component's state. The handleChange method updates the state whenever the user types a new character, and the handleSubmit method alerts the user with the submitted name.

Uncontrolled Components

Another way to work with React Forms is through Uncontrolled Components. Uncontrolled Components allow you to render form elements without controlling them. The data is accessed through the DOM instead of the component's state.


import React, { Component } from 'react';

class App extends Component {
  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>
    );
  }
}

export default App;

This code creates the same form as the previous example, but instead of storing the value in the component's state, it uses the ref attribute to access the input value directly. The handleSubmit method accesses the input value using this.input.value.

Conclusion

React Forms are a crucial part of building web applications that require user input. Controlled Components and Uncontrolled Components are two ways to work with React Forms. Controlled Components use the component's state to control form elements, while Uncontrolled Components use the DOM to access form data.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe