Does a component / page refresh when a form is submitted?
Unanswered
Wuchang bream posted this in #help-forum
Wuchang breamOP
Heyo,
I have a page / component that looks like this (simplified)
Whenever the form submits successfully, it resets the form. What I'm confused on is I thought that
I have a page / component that looks like this (simplified)
"use client";
import Notification from "@/components/notification";
import { useFormState } from "react-dom";
import { useState, useRef, useEffect } from "react";
import { contactFormSubmit } from "@/app/actions";
const initialState = {
success: false,
message: "",
};
export default function Contact() {
const formRef = useRef<HTMLFormElement>(null);
const [show, setShow] = useState(false)
const [state, formAction] = useFormState(contactFormSubmit, initialState);
if (state?.success) {
formRef.current?.reset();
}
useEffect(() => {
if (state.success) {
setShow(true);
}
}, [state.success]);
return (
<>
<form
action={formAction}
ref={formRef}
className="px-6 pb-24 pt-20 sm:pb-32 lg:px-8 lg:py-48"
>
</form>
<Notification show={show} setShow={setShow} state={state} />
</>
)
}Whenever the form submits successfully, it resets the form. What I'm confused on is I thought that
if (state?.success) {
formRef.current?.reset();
} would only run when the pages loads right? Or does it rerun once a form is submitted?21 Replies
Black carp
Why would you do that in your render scope?
I would expect that form reset to be from a useEffect. Using the useFormState hook makes the form I believe not POST hard and would leave you on the page
@Wuchang bream Heyo,
I have a page / component that looks like this (simplified)
ts
"use client";
import Notification from "@/components/notification";
import { useFormState } from "react-dom";
import { useState, useRef, useEffect } from "react";
import { contactFormSubmit } from "@/app/actions";
const initialState = {
success: false,
message: "",
};
export default function Contact() {
const formRef = useRef<HTMLFormElement>(null);
const [show, setShow] = useState(false)
const [state, formAction] = useFormState(contactFormSubmit, initialState);
if (state?.success) {
formRef.current?.reset();
}
useEffect(() => {
if (state.success) {
setShow(true);
}
}, [state.success]);
return (
<>
<form
action={formAction}
ref={formRef}
className="px-6 pb-24 pt-20 sm:pb-32 lg:px-8 lg:py-48"
>
</form>
<Notification show={show} setShow={setShow} state={state} />
</>
)
}
Whenever the form submits successfully, it resets the form. What I'm confused on is I thought that
ts
if (state?.success) {
formRef.current?.reset();
}
would only run when the pages loads right? Or does it rerun once a form is submitted?
When using the
action prop the form reset happens automatically, if you want to avoid that use event handlers instead of the action prop on forms@Plague When using the `action` prop the form reset happens automatically, if you want to avoid that use event handlers instead of the `action` prop on forms
Wuchang breamOP
Really? I'm not sure it does reset automatically
@Black carp I would expect that form reset to be from a useEffect. Using the useFormState hook makes the form I believe not POST hard and would leave you on the page
Wuchang breamOP
I could use useEffect, but if I was listening to when success is true, and the user submits the form once (which makes it success) and then a second time, it wouldn't clear the form
Black carp
Well that is an issue that the useFormState doesn’t reset to initial state, no?
So you’d use a react key to remount the component
@Wuchang bream Really? I'm not sure it does reset automatically
Oh my mistake, that feature is only in the Next.js RC/Canary, they're moving in that direction though for Next.js 15
Wuchang breamOP
ah I see. I don't know if that works in my favour though xD
Since if the form returns an error and it resets it isn't ideal
but thanks for letting me know
@Black carp Well that is an issue that the useFormState doesn’t reset to initial state, no?
Wuchang breamOP
Well yeah I guess the issue is it never changes from success: true, to success: false if I am understanding you right
@Black carp So you’d use a react key to remount the component
Wuchang breamOP
Ah okay I can have a google of this
Black carp
Long story short, it’s a “shortcoming” of the useFormState hook, no way to reset that state (which is the result of a submission).
Wuchang breamOP
Ah gotcha
Thank you for all the help too
Black carp
How’d it go? I want to help 😂
Wuchang breamOP
I haven't got round to it yet lol sorry, I'm trying to finish this Google calendar integration I'm working on before I tackle this (since technically it works fine even though it's not ideal)
I am in desperate need of help though for this Google calendar integration since I've tried hundreds of things yet I can't get it to work haha
Wuchang breamOP
If you want to help on that, https://nextjs-forum.com/post/1282031140886282363 , however it is not really related to next JS so you might not know
Black carp
No worries, i read through and will try to help as I can 😄