Skip to content
the poor coder

react forms

Poor Man
Published
Reading time
2 min read
On this page

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.

JavaScript

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.

JavaScript

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.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC