admin管理员组文章数量:1432175
I'm using Next.js
with form actions to set up a simple form that gets submitted to the server (with a react server action). I can see the form data being received on the server. Unfortunately, once the server action is finished, the form is emptied and the user input is lost.
Any idea why this happens?
Here's the code (I omitted the validation logic for simplicity):
/// Form.tsx
'use client';
import { submitForm } from '../actions/validateFormAction';
export function Form() {
return (
<form action={submitForm}>
<input name="firstName" id="lastName" placeholder="First name" />
<input name="firstName" id="lastName" placeholder="Last name" />
<input type="submit" />
</form>
);
}
/// validateFormAction.ts
'use server';
export async function submitForm(data: FormData): Promise<void> {
console.log('submitForm action', data);
}
Before submitting:
Server console output:
[1] submitForm action FormData {
[1] [Symbol(state)]: [
[1] {
[1] name: '$ACTION_ID_4064bf5eac761f0b8f1771e13eed9498134e43b11e',
[1] value: ''
[1] },
[1] { name: 'firstName', value: 'my first name' },
[1] { name: 'firstName', value: 'my last name' }
[1] ]
[1] }
After submitting:
The browser console mentions [Fast Refresh] rebuilding
(although I don't know whether this is relevant).
Using "next": "15.0.2"
and "react": "19.0.0-rc-02c0e824-20241028"
.
Any help would be appreciated.
I'm using Next.js
with form actions to set up a simple form that gets submitted to the server (with a react server action). I can see the form data being received on the server. Unfortunately, once the server action is finished, the form is emptied and the user input is lost.
Any idea why this happens?
Here's the code (I omitted the validation logic for simplicity):
/// Form.tsx
'use client';
import { submitForm } from '../actions/validateFormAction';
export function Form() {
return (
<form action={submitForm}>
<input name="firstName" id="lastName" placeholder="First name" />
<input name="firstName" id="lastName" placeholder="Last name" />
<input type="submit" />
</form>
);
}
/// validateFormAction.ts
'use server';
export async function submitForm(data: FormData): Promise<void> {
console.log('submitForm action', data);
}
Before submitting:
Server console output:
[1] submitForm action FormData {
[1] [Symbol(state)]: [
[1] {
[1] name: '$ACTION_ID_4064bf5eac761f0b8f1771e13eed9498134e43b11e',
[1] value: ''
[1] },
[1] { name: 'firstName', value: 'my first name' },
[1] { name: 'firstName', value: 'my last name' }
[1] ]
[1] }
After submitting:
The browser console mentions [Fast Refresh] rebuilding
(although I don't know whether this is relevant).
Using "next": "15.0.2"
and "react": "19.0.0-rc-02c0e824-20241028"
.
Any help would be appreciated.
Share Improve this question edited Nov 25, 2024 at 8:58 fikkatra asked Nov 18, 2024 at 21:53 fikkatrafikkatra 5,8424 gold badges44 silver badges74 bronze badges 8 | Show 3 more comments1 Answer
Reset to default 4After raising the issue with the Next.js team on their Github, they replied that this behavior is React-related and by design. There's an open discussion on the React Github and a feature request to opt out of the form resetting behavior. The discussion also contains some workarounds, one of which suggests to do this:
function handleSubmit(event) {
event.preventDefault();
const formData = new FormData(event.target);
startTransition(() => action(formData));
}
...
<form onSubmit={handleSubmit}>
instead of:
<form action={action}>
本文标签: reactjsNextjs forms with server action form gets emptied after submitStack Overflow
版权声明:本文标题:reactjs - Next.js forms with server action: form gets emptied after submit - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745591886a2665250.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
rebuilding
). When the page is reloaded, the form is cleared. If you're wanting to stop this behavior, maybe catching the event and preventing the propagation solves it – Alexander Santos Commented Nov 18, 2024 at 22:09onChange
event or you can get the data in the action and send it back withconst [state, action] = useActionState()
– Evilolipop Commented Nov 26, 2024 at 9:02const [state, action] = useActionState(ac, initialState)
, then<input name="firstName" id="firstName" defaultValue={state.firstName} />
– Evilolipop Commented Nov 28, 2024 at 10:06