How to reset the form default values to loader data after successful submission? #352
Unanswered
hauptrolle
asked this question in
Q&A
Replies: 2 comments
-
|
I have found a similar issue: #129 Do I really have to check for equality in a useEffect(() => {
if (JSON.stringify(loaderData) !== JSON.stringify(form.initialValue)) {
formRef.current?.reset();
}
}, [loaderData, form.initialValue]); |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
You can reset the form by marking payload on the submission as export let action = async ({ request }: ActionArgs) => {
const formData = await request.formData();
const submission = parse(formData, { schema });
if (submission.intent !== 'submit' || !submission.value) {
return json(submission);
}
return json({
...submission,
// Notify the client to reset the form using `null`
payload: null,
});
};
export default function Component() {
const lastSubmission = useActionData<typeof action>();
const [form, { message }] = useForm({
// The last submission should be updated regardless the submission is successful or not
// If the submission payload is empty:
// 1. the form will be reset automatically
// 2. the default value of the form will also be reset if the document is reloaded (e.g. nojs)
lastSubmission,
})
// ...
}But you are still required to reset the form manually in case of normal navigation. For example, if you were editing article a (e.g. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How do I reset the form default values to the new default values from the loader after a successful form submission?
Example:
Beta Was this translation helpful? Give feedback.
All reactions