-
Notifications
You must be signed in to change notification settings - Fork 27k
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
Forms with server action: form gets emptied after submit #72949
Comments
Hey, that' s just the normal behavior of server actions inside next js, the solution would be to simply save the user input in a state (of course it has to be a client component) then set the value of the input to that state: . The value inside the input would be lost when the user clicks on submit but it will then be recovered by the state. i forked your repo and took liberty of adding some changes and using best practices. Hope you like it : "https://github.com/Marabii/next-form-poc". If you have any additional questions reach out to me. |
Thanks for the clarification and the example @Marabii. In reality I'm trying to combine client side validation with In any case: thanks for clarifying. I'll close this issue, since it's not really a bug. |
but the documentation in the part of server actions and mutations says this,, (After hydration, the browser does not refresh on form submission.).,, i have also fix the issues but like fikkatra i want it sever side and to fix it i have to become it client |
I found this blog a while ago : "https://nehalist.io/react-hook-form-with-nextjs-server-actions/" hope you find it useful @fikkatra. You may want to use the useActionState hook instead of useFormStatus they used on the blog. |
Yeah, I saw that one too and followed their instructions, but then I bumped into the emptied form issue... I thought it might have something to do with a newer or more recent next or react feature, since the blog still uses useFormStatus. So that brought me here in the first place 🤷♀️. They are also not storing the form values. Unless I overlooked something, the blog's implementation will have the same behavior upon submitting to the server, the form will be empty. Thanks a lot for sharing though, and for taking the time to help me out! |
I am also facing this problem in the next 15. { "next": "15.0.2",
"react": "19.0.0-rc-02c0e824-20241028",
"react-dom": "19.0.0-rc-02c0e824-20241028"
} But it worked fine in : {
"next": "14.2.13",
"react": "^18",
"react-dom": "^18",
} |
this is my solution but you have to use the "use client"
and in the form you call your server action as normal |
Since it worked in a previous version according to @thatanjan , I'm reopening this issue. |
Link to the code that reproduces this issue
https://github.com/fikkatra/next-form-poc
To Reproduce
Current vs. Expected behavior
I expect the form to contain the original data, since I am not resetting the form or redirecting. This is important in case of form validation errors (which I didn't include to keep the code simple).
Before submit:
Server console output:
After submit:
Browser console:
[Fast Refresh] rebuilding
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 24.1.0: Thu Oct 10 21:03:15 PDT 2024; root:xnu-11215.41.3~2/RELEASE_ARM64_T6000 Available memory (MB): 32768 Available CPU cores: 8 Binaries: Node: 18.20.4 npm: 10.7.0 Yarn: 1.22.19 pnpm: N/A Relevant Packages: next: 15.0.3 // Latest available version is detected (15.0.3). eslint-config-next: 15.0.3 react: 19.0.0-rc-66855b96-20241106 react-dom: 19.0.0-rc-66855b96-20241106 typescript: 5.6.3 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Not sure
Which stage(s) are affected? (Select all that apply)
next dev (local), next build (local), next start (local)
Additional context
I'm not using a canary version.
The text was updated successfully, but these errors were encountered: