React Forms

In React, form data is usually handled by the components.

31st July 2022

Time to read: 1 min

Logo

Handling Forms

Use the useState Hook to manage the input:

import React, { useState } from 'react'

function MyForm() {
  const [name, setName] = useState("");

  return (
    <form>
      <label>Enter your name:
        <input
          type="text" 
          value={name}
          onChange={(e) => setName(e.target.value)}
        />
      </label>
    </form>
  )
}

Submitting Forms

Control the submit action by adding an event handler in the onSubmit attribute for the <form>:

import React, { useState } from 'react'

function MyForm() {
  const [name, setName] = useState("");

  const handleSubmit = (event) => {
    event.preventDefault();
    alert(`The name you entered was: ${name}`)
  }

  return (
    <form onSubmit={handleSubmit}>
      <label>Enter your name:
        <input 
          type="text" 
          value={name}
          onChange={(e) => setName(e.target.value)}
        />
      </label>
      <input type="submit" />
    </form>
  )
}

Multiple Input Fields

Control the values of more than one input field by adding a name attribute to each element.

import React, { useState } from 'react'

function MyForm() {
  // initialize state with an empty object
  const [inputs, setInputs] = useState({});

  const handleChange = (event) => {
    const name = event.target.name;
    const value = event.target.value;
    setInputs(values => ({...values, [name]: value}))
  }

  const handleSubmit = (event) => {
    event.preventDefault();
    alert(inputs);
  }

  return (
    <form onSubmit={handleSubmit}>
      <label>Enter your name:
      <input 
        type="text" 
        name="username" 
        value={inputs.username || ""} 
        onChange={handleChange}
      />
      </label>
      <label>Enter your age:
        <input 
          type="number" 
          name="age" 
          value={inputs.age || ""} 
          onChange={handleChange}
        />
        </label>
        <input type="submit" />
    </form>
  )
}

Textarea

In React the value of a textarea is placed in a value attribute.

import React, { useState } from 'react'

function MyForm() {
  const [textarea, setTextarea] = useState(
    "The content of a textarea goes in the value attribute"
  );

  const handleChange = (event) => {
    setTextarea(event.target.value)
  }

  return (
    <form>
      <textarea value={textarea} onChange={handleChange} />
    </form>
  )
}

Select

In React, the selected value is defined with a value attribute on the select tag:

function MyForm() {
  const [myCar, setMyCar] = useState("Volvo");

  const handleChange = (event) => {
    setMyCar(event.target.value)
  }

  return (
    <form>
      <select value={myCar} onChange={handleChange}>
        <option value="Ford">Ford</option>
        <option value="Volvo">Volvo</option>
        <option value="Fiat">Fiat</option>
      </select>
    </form>
  )
}

Checkbox

function MyForm() {
  const [theme, toggleTheme] = useState("light");

  return (
    <input
     type="checkbox"
     onChange={e => toggleTheme(e.target.checked ? 'dark' : 'light')}
     checked={theme === 'dark'} />
  )
}