Replies: 9 comments 15 replies
-
Code example? Forms have a reset method, or you could alternatively have a button with type "reset", but without a code example, it is a bit hard to guide you in the right direction. |
Beta Was this translation helpful? Give feedback.
-
Trying to figure out the same thing and not finding anything in the docs. There's no reset method on FormData at the moment. Using the latest version of next (currently 14.0.3).
|
Beta Was this translation helpful? Give feedback.
-
Any new things anybody found here? I want to be able to reset the form in my server component. |
Beta Was this translation helpful? Give feedback.
-
Nevermind. As mentioned in the video of Lee Robinson (at around minute 18) https://www.youtube.com/watch?v=8DyzsSWNcDE&ab_channel=LeeRobinson we lose nothing if we use a client component instead of a server component so it makes sense to make the form a client component. |
Beta Was this translation helpful? Give feedback.
-
https://react.dev/reference/react-dom/components/form#optimistically-updating-form-data And if you use
|
Beta Was this translation helpful? Give feedback.
-
I created an issue on the react side since this seems to be an open point. |
Beta Was this translation helpful? Give feedback.
-
i get the solution
you can check the full code in my github link |
Beta Was this translation helpful? Give feedback.
-
Another way to reset the form is by using the prop key. I'm not entirely sure how recommended this approach is, but you can return a new state from the action and then use a key to reset the form. Here's a basic example: "use client";
import { useFormState } from "react-dom";
import { addPost } from "./actions";
export default function Home() {
const [state, action] = useFormState(addPost, { error: null });
return (
<main>
<form action={action} key={state?.resetKey}>
<input type="text" placeholder="Title" name="title" />
<button>Submit</button>
</form>
</main>
);
} "use server";
import { redirect } from "next/navigation";
export const addPost = (prevState, formData: FormData) => {
const title = formData.get("title")?.toString();
if (!title) {
return {
resetKey: prevState.resetKey,
error: "Title is required",
};
}
const post = createPost(title);
return {
error: null,
data: post,
resetKey: post.id,
};
};
function createPost(title: string) {
return {
id: Date.now(),
title,
};
} |
Beta Was this translation helpful? Give feedback.
-
Any new solution to this? I have a "use server" form and I can't clear my form. Seems like it should be something trivial right? |
Beta Was this translation helpful? Give feedback.
-
Summary
Hey,
How to clear / reset input after server mutation ?
I don't see any tutorial or so.
Please help.
Many Thanks!
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions