-
Notifications
You must be signed in to change notification settings - Fork 47.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[React 19] Controlled <select>
component is subject to automatic form reset
#30580
Comments
@cjg1122 |
Since Here's an updated version of the code that properly handles resetting both the "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:
By incorporating these changes, the form submission will reset both the |
I tried with above example and getting this error I think it happed due to mismatching of args for useActionState src: https://react.dev/reference/react/useActionState https://codesandbox.io/p/sandbox/react-dev-forked-qvpx9c?file=%2Fsrc%2FPage.js |
@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.~ |
@Deba0099 I tried to setState the local state after the action is submitted, but the select component is still reset to the default value. |
<select>
component is subject to automatic form reset
This is indeed a bug, thank you for reporting. Only uncontrolled components should be automatically reset when the Screen.Recording.2024-08-02.at.13.45.45.mov |
@cjg1122 |
@cjg1122
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 |
Is this bug still present? I could work on it in this case |
No, it's not.
…On Sun, 13 Oct 2024 at 2:09 PM, Daniele Berardi ***@***.***> wrote:
Is this bug still present? I could work on it in this case
—
Reply to this email directly, view it on GitHub
<#30580 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/A5IUT46JUCTTIEXSKUHYSETZ3IWTZAVCNFSM6AAAAABL4HOOQSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIMBYHA4DKMZZHE>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
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 |
Now that React 19 is officially out, I can confirm this bug still persists. |
yea same here |
The controlled component also resets the "select" after the action is triggered.
but the "input" component does not.
Repro: https://codesandbox.io/p/sandbox/stupefied-cohen-n578l6
The text was updated successfully, but these errors were encountered: