React

Stop using React useState to control inputs

Feb 20, 2023Updated Nov 24, 20262 min readRami Mustaklem

Updated November 2026: added a section on React 19 form actions.

Why

Each update in a react component causes the whole component to re-render. Imagine you have a form with a few inputs, like name, age and date of birth. When you use the onChange input attribute to track the input value into a component state, the onChange updates the state and thus the whole component re-renders to update the input value, and so on.

Unless strictly necessary you want to watch or track an input value, try to extract the input and its state into its own component in order to only re-render this component itself without the surrounding elements.

To be fair, for a small form this re-render is cheap, and controlled inputs are still the right choice when you need live validation, or to show and hide parts of the UI based on what the user types. But most of the time, you don’t need to track every keystroke.

Solution/s

If no watching of fields is needed, try using a useRef hook or nothing at all when using inputs. Instead, wrap inputs in its own form element, along with the submit input/button, which also gives the benefit for using the “enter” key to submit and when submitting the form you can extract all input values without re-rendering the component.

When using a form element, there’s no need to custom implement the “enter” key submit, and also adding a required attribute to inputs prevents the form from submitting.

The only thing needed is to prevent the default behavior of the form submission event, and if needed reset the form all at once.

If you have more than a few inputs or forms in your application, I would recommend using a form library like react-hook-form in order to implement well optimized forms that do not trigger unnecessary re-renders, with the option to watch input values and many other functionalities.

Conclusion

If a component is a form and it has a few inputs with onChange function, it will re-render the whole component once a change happens.

Examples

Using Form onSubmit function without any state or ref:

import { FormEvent } from "react";

export default function FormFields() {
  const onFormSubmitHandler = (e: FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    const form = e.currentTarget;
    const data = Object.fromEntries(new FormData(form));
    console.log(data); // { field1: "...", field2: "...", field3: "..." }
    form.reset();
  };

  return (
    <form onSubmit={onFormSubmitHandler}>
      <label htmlFor="field-1">
        <span>Field 1</span>
        <input type="text" id="field-1" name="field1" required />
      </label>

      <label htmlFor="field-2">
        <span>Field 2</span>
        <input type="text" id="field-2" name="field2" required />
      </label>

      <label htmlFor="field-3">
        <span>Field 3</span>
        <input type="text" id="field-3" name="field3" required />
      </label>

      <div>
        {/* Default role for the button inside a form is submit */}
        {/* Either this or use <input type="submit" /> */}
        <button>Submit</button>
      </div>
    </form>
  );
}

Using React Hook Form library:

If you need to watch a field value while the user types, don’t call watch() at the top of the form component, because that re-renders the whole form on every keystroke, which is exactly what we’re trying to avoid. Instead, use the useWatch hook inside a separate small component and pass it the form’s control, this way only that component re-renders when the watched field changes. Calling watch() is still fine when you only read a value inside an event handler.

import { useForm, useWatch, type Control } from "react-hook-form";

type MyFields = { firstName: string; lastName: string; username: string };

// Only this component re-renders when firstName changes
function FirstNamePreview({ control }: { control: Control<MyFields> }) {
  const firstName = useWatch({ control, name: "firstName" });
  return <p>Hello {firstName}</p>;
}

export default function HookForm() {
  const { register, handleSubmit, control } = useForm<MyFields>({
    // Without default values, useWatch returns undefined on the first render
    defaultValues: { firstName: "", lastName: "", username: "" },
  });

  const onSubmit = (data: MyFields) => console.log(data);

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <label>
        <span>First name</span>
        <input type="text" {...register("firstName", { required: true })} />
      </label>

      <label>
        <span>Last name</span>
        <input type="text" {...register("lastName", { required: true })} />
      </label>

      <label>
        <span>Username</span>
        <input type="text" {...register("username", { required: true })} />
      </label>

      <FirstNamePreview control={control} />

      <div>
        <button>Submit</button>
      </div>
    </form>
  );
}

Update (November 2026): React 19 form actions

Since I wrote this article, React 19 was released with built-in support for form actions, and this is how I’d do it today. You can now pass a function to the form’s action prop, React passes it the FormData directly, and resets the uncontrolled inputs automatically once the action is done, so there’s no need for preventDefault() or reset() anymore. The approach above still works, this is just less code.

export default function FormFields() {
  const submitAction = async (formData: FormData) => {
    console.log(Object.fromEntries(formData));
  };

  return (
    <form action={submitAction}>
      <label htmlFor="field-1">
        <span>Field 1</span>
        <input type="text" id="field-1" name="field1" required />
      </label>

      <button>Submit</button>
    </form>
  );
}

If you need a pending state or to show errors returned from the action, take a look at the useActionState and useFormStatus hooks.


Do you still use npm? Read my take here on why I use pnpm over npm and yarn.

R
Rami Mustaklem
Senior Full Stack Developer