Skip to content

React handler onChange

Published: at 08:57 AM

Understanding onChange in React:

The onChange event handler is crucial for handling user input changes in various form elements like text inputs, checkboxes, radio buttons, and more. It triggers whenever the value of the element changes, allowing you to react to those changes and update your component’s state accordingly.

Key Considerations:

Common approaches to handling onChange in React:

  1. Single Input with String/Number Value:

    • Create a state variable to store the input value.
    • Use the onChange handler to update the state variable using the event’s target.value.
    const [inputValue, setInputValue] = useState("");
    
    const handleChange = event => {
      setInputValue(event.target.value);
    };
    
    return <input type="text" value={inputValue} onChange={handleChange} />;
  2. Single Input with Checkbox/Radio Button:

    • Use the checked attribute and state variable to reflect the checked state.
    • Update the state using the event’s target.checked value in the onChange handler.
    const [isChecked, setIsChecked] = useState(false);
    
    const handleChange = event => {
      setIsChecked(event.target.checked);
    };
    
    return <input type="checkbox" checked={isChecked} onChange={handleChange} />;
  3. Multiple Inputs with Shared Handler:

    • Pass an identifier (e.g., name attribute) or index to differentiate inputs.
    • Use the target.name or target.index in the onChange handler to identify the changed input and update the corresponding state property.
    const [inputs, setInputs] = useState({ name: "", email: "" });
    
    const handleChange = event => {
      setInputs({ ...inputs, [event.target.name]: event.target.value });
    };
    
    return (
      <form>
        <input
          type="text"
          name="name"
          value={inputs.name}
          onChange={handleChange}
        />
        <input
          type="email"
          name="email"
          value={inputs.email}
          onChange={handleChange}
        />
      </form>
    );

Avoiding Repetitive Handler Functions:

Another one, without repetitive handler onChange function, this approach can be one of solutions

import React, { useState } from "react";

const Navbar = props => {
  return <input type="text" onChange={props.handlerChange("searchForm1")} />;
};

const View = props => {
  return <pre>{JSON.stringify(props.view, null, 2)}</pre>;
};

export default function App() {
  const [search, setSearch] = useState({
    searchForm1: "",
    searchForm2: "",
  });

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

  return (
    <>
      <Navbar handlerChange={handleChange} />
      <View view={search} />
    </>
  );
}

Choosing the Right Approach:

The best approach depends on your specific use case and complexity. Consider factors like component reusability, maintainability, and performance when making your decision.

I hope this comprehensive explanation helps you effectively handle onChange events in your React applications!