Skip to content

[React 19] Controlled <select> component is subject to automatic form reset #30580

Open

Description

@cjg1122

The controlled component also resets the "select" after the action is triggered.
but the "input" component does not.

"use client";
import { useActionState, useState } from "react";
function add() {
  return Date.now();
}
export default function Page() {
  const [state, formAction] = useActionState(add, 0);
  const [name, setName] = useState("");
  const [type, setType] = useState("2");
  return (
    <form action={formAction}>
      <p>{state}</p>
      <p>
        <input
          type="text"
          value={name}
          onChange={(e) => setName(e.target.value)}
        />
      </p>
      <p>
        <select
          name="gender"
          value={type}
          onChange={(e) => setType(e.target.value)}
        >
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
      </p>
      <button>submit</button>
    </form>
  );
}

20240802-193357

Repro: https://codesandbox.io/p/sandbox/stupefied-cohen-n578l6

Activity

  1. ujshaikh commented on Aug 2, 2024

    @ujshaikh

    @cjg1122
    Can you please add actual and expected behavior?

  2. Deba0099 commented on Aug 2, 2024

    @Deba0099

    Since useActionState passes the previous state and returns the updated state, we can reset the input and select values accordingly.

    Here's an updated version of the code that properly handles resetting both the input and select elements:

    "use client";
    import { useActionState, useState } from "react";
    
    // This function handles the form submission
    function add(previousState, formData) {
      // Process the formData and return the new state
      const newState = Date.now();
      // Reset the input and select values
      formData.reset();
      return newState;
    }
    
    export default function Page() {
      // useActionState hook manages the form state and action
      const [state, formAction] = useActionState(add, 0);
    
      // Local state for input and select values
      const [name, setName] = useState("");
      const [type, setType] = useState("2");
    
      return (
        <form
          action={(e) => {
            formAction(e);  // Call the form action
            setName("");    // Reset the input value
            setType("2");   // Reset the select value
          }}
        >
          <p>{state}</p>
          <p>
            <input
              type="text"
              value={name}
              onChange={(e) => setName(e.target.value)}
            />
          </p>
          <p>
            <select
              name="gender"
              value={type}
              onChange={(e) => setType(e.target.value)}
            >
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
            </select>
          </p>
          <button>submit</button>
        </form>
      );
    }

    Explanation:

    1. add function:

      • Receives previousState and formData as arguments.
      • Processes the form data and returns a new state.
      • Resets the form fields by calling formData.reset(), which is a built-in method to reset form values.
    2. form action handler:

      • Calls formAction to handle the form submission.
      • Resets the local state for the input and select elements to their initial values ("" and "2" respectively).

    By incorporating these changes, the form submission will reset both the input and select elements properly.

  3. ujshaikh commented on Aug 2, 2024

    @ujshaikh

    I tried with above example and getting this error
    image

    I think it happed due to mismatching of args for useActionState
    const [state, formAction] = useActionState(fn, initialState, permalink?);

    src: https://react.dev/reference/react/useActionState

    https://codesandbox.io/p/sandbox/react-dev-forked-qvpx9c?file=%2Fsrc%2FPage.js

  4. cjg1122 commented on Aug 2, 2024

    @cjg1122
    Author

    @ujshaikh The expected behaviour is that the select component should behave the same way as the input component, when I click on the submit button, the action executes and then my 2 components should keep their state values, currently the input component is as expected and the select is reset to its default value instead of the one I chose before I submitted.

    I've uploaded a gif of the demo.~

  5. cjg1122 commented on Aug 2, 2024

    @cjg1122
    Author

    @Deba0099 I tried to setState the local state after the action is submitted, but the select component is still reset to the default value.

  6. changed the title [-][React 19] The controlled component also resets the <select> after the action[/-] [+][React 19] Controlled `<select>` component is subject to automatic form reset[/+] on Aug 2, 2024
  7. eps1lon commented on Aug 2, 2024

    @eps1lon
    Collaborator

    This is indeed a bug, thank you for reporting. Only uncontrolled components should be automatically reset when the action prop is used. The <select> here is controlled though and shouldn't change its value after the form is submitted.

    Screen.Recording.2024-08-02.at.13.45.45.mov

    -- https://codesandbox.io/p/sandbox/stupefied-cohen-n578l6

  8. ujshaikh commented on Aug 2, 2024

    @ujshaikh
  9. AmanVerma2202 commented on Aug 4, 2024

    @AmanVerma2202

    @cjg1122
    fix: reset both input and select fields to default state after form submission

    • Added useEffect to reset the 'name' input field and 'type' select field to their default states after form action is triggered.
     useEffect(() => {
        setName("");
        setType("2"); 
      }, [state]);
    
    "use client";
    import { useActionState, useState, useEffect } from "react";
    
    function add() {
      return Date.now();
    }
    
    export default function Page() {
      const [state, formAction] = useActionState(add, 0);
      const [name, setName] = useState("");
      const [type, setType] = useState("2");
    
      // Reset input and select states when action state changes
      useEffect(() => {
        setName(""); // Reset name input to empty 
        setType("2"); // Reset select to default value 2
      }, [state]);
    
      return (
        <form action={formAction}>
          <p>{state}</p>
          <p>
            <input
              type="text"
              value={name}
              onChange={(e) => setName(e.target.value)}
            />
          </p>
          <p>
            <select
              name="gender"
              value={type}
              onChange={(e) => setType(e.target.value)}
            >
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
            </select>
          </p>
          <button>submit</button>
        </form>
      );
    }
    
    • fix 👍 : reset both fields to default state after form submission using useRef hook
    import { StrictMode, useState, useRef, version } from "react";
    import { createRoot } from "react-dom/client";
    
    function App() {
      const [selectValue, setSelectValue] = useState("One");
      const [textValue, setTextValue] = useState("Alpha");
      const formRef = useRef(null);
    
      return (
        <form
          ref={formRef}
          action={async (formData) => {
            console.log(Object.fromEntries(formData.entries()));
            formRef.current.reset();
            setSelectValue("One");
            setTextValue("Alpha");
          }}
        >
          <label>
            select:
            <select
              name="selectValue"
              onChange={(event) => {
                setSelectValue(event.currentTarget.value);
              }}
              value={selectValue}
            >
              <option>One</option>
              <option>Two</option>
            </select>
            controlled: {selectValue}
          </label>
          <br />
          <label>
            text:
            <input
              name="textValue"
              onChange={(event) => {
                setTextValue(event.currentTarget.value);
              }}
              value={textValue}
            />
            controlled: {textValue}
          </label>
          <br />
          <input type="submit" />
        </form>
      );
    }
    
    const rootElement = document.getElementById("root");
    const root = createRoot(rootElement);
    
    root.render(
      <>
        <p>React: {version}</p>
        <App />
      </>
    );
    
    

    check out code 1 here :https://codesandbox.io/p/sandbox/react-dev-forked-vfpgnn?file=%2Fsrc%2FPage.js%3A20%2C31

    check out code 2 here : https://codesandbox.io/p/sandbox/react-controlled-select-subject-for-form-reset-forked-ng44jt

  10. dberardi99 commented on Oct 13, 2024

    @dberardi99

    Is this bug still present? I could work on it in this case

  11. AmanVerma2202 commented on Oct 13, 2024

    @AmanVerma2202
  12. jeremy-step commented on Nov 28, 2024

    @jeremy-step

    This issue is in fact still present. Including 19.0.0-rc.1, 19.0.0-beta-26f2496093-20240514 and 19.0.0-rc-7670501b-20241124

  13. 18 remaining items

  14. issakujitsuk commented on Jul 23, 2025

    @issakujitsuk

    Using onSubmit instead of the action attribute avoids this issue. The <select> value is preserved when using onSubmit.

  15. gcb commented on Jul 23, 2025

    @gcb

    Using onSubmit instead of the action attribute avoids this issue. The <select> value is preserved when using onSubmit.

    not the point here. if you canceldefault on onSubmit, then the useAction state code don't trigger. This is about when that code triggers and you explicitly set the values, which are not honored later on, as they would if it was a regular serverAction without useActionState.

  16. karolskolasinski commented on Aug 24, 2025

    @karolskolasinski

    Please fix this bug. There is no workaround.

  17. gcb commented on Aug 25, 2025

    @gcb

    Please fix this bug. There is no workaround.

    the workaround is very simple. don't use this and keep on using the old pattern:

    'use client'
    ...
    export default function SomePage ( prop :SomePageProp) {
        const [state, setState] = useState(prop.initialState);
        const [isPending, startTransition] = useTransition();
    
    
        const handleSubmit = async (e: FormEvent<HTMLFormElement>) => {
            e.preventDefault(); // prevent the noJS action
            const formData = new FormData(e.currentTarget);
            try {
                startTransition(async () => {
                    try {
                        const result = await someEntityServerAction(formData);
                        setState(result);
                        ...
                    } catch (error) {
                        console.warn('Server action error', error);
                    }
                });
            } catch (error) {
                console.warn('Client action error', error);
            }
        };
        ...
        return (<>
            <form action={someEntitityNojsFormAction} onSubmit={handleSubmit}>
            ... <button enabled={!isPending} type="submit">...
        </>)
    }
    

    ...but yeah, would be nice if the helper method here worked for more than the "Static Counter Tutorial" example.

    edit: bonus, template for the server-action workaround:

    'use server'
    import 'server-only';
    import { ..., type SomeEntity } '@lib/some_entity.ts';
    ...
    export interface SomeEntityState { // whatever you will return
        message :null|string,
        error: {
            [K in keyof SomeEntity]?: string;
        },
        data :SomeEntity,
    };
    export async function someEntityUpdateNojsFormAction( postData: FormData) :Promise<void> {
        await someEntityUpdateAction(postData);
    }
    export async function someEntityUpdateAction( postData: FormData) :Promise<SomeEntityState> {
        ...
    }
    
  18. olayway commented on Nov 10, 2025

    @olayway

    Please fix this :)

  19. gcb commented on Nov 16, 2025

    @gcb

    better title would be "[react 19+] useActionState fails to maintain controlled values, in several cases, after form submission"

  20. qjstuart commented on Nov 26, 2025

    @qjstuart

    I've also run into this issue. I'm on a Mac - noticing that when I swipe away from the browser (3-finger swipe on trackpad) to another desktop, then back to the browser, the select box updates correctly. Has anyone else noticed similar behaviour?

  21. albr74 commented on Dec 18, 2025

    @albr74

    I have the same problem

  22. added a commit that references this issue on Dec 19, 2025
    0018d36
  23. added a commit that references this issue on Dec 30, 2025
    70c1ae3
  24. karlhorky commented on Jan 21, 2026

    @karlhorky
    Contributor

    @eps1lon thanks for making the PR #35168 for this in Nov 2025, great to see an official PR fixing it 🚀 I'm watching that PR now 👀

  25. karlhorky commented on Jan 21, 2026

    @karlhorky
    Contributor

    Workarounds

    For anyone waiting, some workarounds that you may want to try:

    1. Use a key prop on the <select> or component containing it
    2. @gcb's useTransition() workaround further up in this issue: [React 19] Controlled <select> component is subject to automatic form reset #30580
    3. @danieltott's useEffect workaround from the other issue: [React 19] allow opting out of automatic form reset when Form Actions are used #29034 (comment)
  26. valerii15298 commented on Apr 29, 2026

    @valerii15298

    So many nasty bugs with react action and useActionState...
    I am transitioning to use plain old onSubmit which works reliable and recommend others to do the same. Such kind of tricky bugs are just not worth to fight when they are in the core and not fixed for years

    I even consider to move to Solid because of bugs like this(also not sure if that path will not have its own bugs).
    I love React with its pretty robust and stable releases over the years, and respect the team making it.

    But seems like reliability is no longer a priority for the React project, and focus is more on quantity of features over quality
    🙁

  27. LutherTS commented on Apr 29, 2026

    @LutherTS

    @valerii15298 Just don't use action and useActionState. As you can see, this is an issue we have been complaining about since 2024. Combine useState, startTransition and onSubmit, and not only will you not have to deal with the factually broken APIs you've mentioned, you'll even have a lot more leeway about how to control your form submissions.

  28. karlhorky commented on Sep 30, 2026

    @karlhorky
    Contributor

    This also affects Next.js (await cookies()).set() in a very confusing way (all values update except the <select> value) - I created a reproduction for that here:

    Screen.Recording.2026-09-30.at.15.32.28.mov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions