Skip to content
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

Open
fikkatra opened this issue Nov 19, 2024 · 8 comments
Open

Forms with server action: form gets emptied after submit #72949

fikkatra opened this issue Nov 19, 2024 · 8 comments
Labels
bug Issue was opened via the bug report template.

Comments

@fikkatra
Copy link

fikkatra commented Nov 19, 2024

Link to the code that reproduces this issue

https://github.com/fikkatra/next-form-poc

To Reproduce

  1. Start the app by running npm run dev
  2. Open the app in the browser at http://localhost:3000
  3. Fill in the form and click the submit button
  4. Result: the form is emptied after the submit. All input is lost.

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:
image

Server console output:

submitForm action FormData {
  [Symbol(state)]: [
    {
      name: '$ACTION_ID_40d23cd8bd75560ac5be4fdcb74a7a5de0950cbc8e',
      value: ''
    },
    { name: 'firstName', value: 'My first name' },
    { name: 'lastName', value: 'My last name' }
  ]
}
 POST / 200 in 46ms

After submit:
image

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.

@fikkatra fikkatra added the bug Issue was opened via the bug report template. label Nov 19, 2024
@Marabii
Copy link

Marabii commented Nov 19, 2024

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.

@fikkatra
Copy link
Author

Thanks for the clarification and the example @Marabii. In reality I'm trying to combine client side validation with react-hook-form and zod, and server-side validation (with zod + custom error cases). I have yet to find a blogpost or tutorial that combines these frameworks, I'm really struggling to get all of them to work nicely together. I'm surprised, since all of these libs are industry standards (or at least very popular), and combining client and serverside validation is also a best practice...

In any case: thanks for clarifying. I'll close this issue, since it's not really a bug.

@antdev1987
Copy link

antdev1987 commented Nov 20, 2024

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

@Marabii
Copy link

Marabii commented Nov 20, 2024

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.

@fikkatra
Copy link
Author

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!

@thatanjan
Copy link

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",
}

@antdev1987
Copy link

antdev1987 commented Nov 21, 2024

this is my solution but you have to use the "use client"
this is the handle submit

  const handleSubmit = (e: React.FormEvent<HTMLFormElement>)=>{
    e.preventDefault(); // Evita el reload de la página
    const formData = new FormData(e.currentTarget);
    createEmployee(formData); // Envía los datos al server action
  }

and in the form you call your server action as normal
form action={createEmployee} onSubmit={handleSubmit}
createEmployee is the server action

@fikkatra
Copy link
Author

Since it worked in a previous version according to @thatanjan , I'm reopening this issue.

@fikkatra fikkatra reopened this Nov 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

4 participants