-
Notifications
You must be signed in to change notification settings - Fork 26.1k
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
[NEXT-1168] useFormStatus doesn't seem to work with server actions #49232
Comments
Moreover, the terminal also logs an error:
Even though the initial state is correctly displayed in the browser. |
Just hit this too |
I was able to get rid of the error by installing the |
Found out that Still getting the error in the server console though, even after upgrading next/react/react-dom. |
I have an incredibly simple component, hitting the Installed react@next and react-dom@next and no change unfortunately. |
Also having the same issue. |
Same error here! |
EditI managed to get rid of the {
"react": "experimental",
"react-dom": "experimental",
"next": "canary"
} But it's only for client components. So I get that error on server components.
OriginalHow did you manage to get rid of {
"react": "next",
"react-dom": "next",
"next": "canary"
} |
Thanks, I got it to work with this comment without having to resort to full-experimental mode, i.e. this works: // app/.../page.tsx
"use server";
import { ClientComponent } from "./ClientComponent";
async function doSomething() {}
export default function Page() {
return <form action={doSomething}><ClientComponent /></form>;
} // app/.../ClientComponent.tsx
"use client";
import { experimental_useFormStatus as useFormStatus } from "react-dom";
export function ClientComponent() {
const formStatus = useFormStatus();
return <input type="submit" disabled={formStatus.pending}>Go</input>;
} |
@Gregoor Yeah that works because you're using it in a client component. Documentation doesn't state that: |
@Gregoor You found a way to retrieve the response from the server action? I want to receive if had an error on server-side, and display that on my client component. |
any idea why the pending flag does not update when the form is submitting? It’s always false. |
Yes-ish, but it did not work when
Not yet, but I'd like to know that too |
So... what is the recommended way of showing a loading status for server-component form submissions? |
The only solution that has worked for me is having the You can find a full working example here: https://github.com/techulus/manage-prompt/blob/c9dad49a802c2192d1553d62e4b3151dda134fdf/app/console/workflows/new/page.tsx#L7 |
Is there a way for the form to avoid rerender & the |
Documentation has been updated with NextJS v13.4.5. |
Just in case if anyone is getting caught on the part where return value is not updated for a submitted form, I wrote a small utility that could help you with that. It allows you to lift the state of https://gist.github.com/JLarky/190bab52ff13c44f9420523d1792fbf0 |
Thank you! I appreciate the gist -- it worked great. |
In next.config.js enable server actions.Don't forget to use npm run dev to build the project again. This worked for me.
|
This worked for me too |
Did anyone figure this out? I'm struggling to get this repo working with this error: https://github.com/steven-tey/prisma-server-actions |
I have tried a lot of different ways, but the API is definitely experimental and breaks. So, reverted back to not using form action for complex form processing. |
I got a TS error when trying to import: import { experimental_useFormStatus as useFormStatus } from "react-dom"; When ignoring this error, problem seems to be solved // ts-ignore because experimental_useFormStatus is not in the types
// @ts-ignore
import { experimental_useFormStatus as useFormStatus } from "react-dom"; |
Does anyone knows how to make this work with jest+testing library and with storybook, in both integration and visual tests i am getting |
Has anyone found a way to get around that ? what if I want to have export default function Contact() {
const [state, formAction] = useFormState(myAction, initialState);
const { pending } = useFormStatus();
return <div>
{pending ? <h1>Loading...<h1> : ''}
<div>
...
<form action={formAction}>
.... |
@panoskouff "use client"
export default function SubmitButton() {
const { pending } = useFormStatus()
return (
<button type="submit" disabled={pending}>{pending ? 'Loading...' : 'Submit'}</button>
)
} export default function Contact() {
return (
<form action={formAction}>
...
<SubmitButton />
</form>
)
} If you don't want anything to be visible from the form, you could make all your inputs part of a client component. "use client"
export default FormInputs() {
const { pending } = useFormStatus()
return (
<>
{pending ? (
<h1>Loading...<h1>
) : (
<>
<label><input /></label>
...
</>
)}
</>
)
} |
Adding my learnings going through this. The documentation of NextJS is really not complete. Best would be to watch the video on Youtube: https://www.youtube.com/watch?v=dDpZfOQBMaU and check its repo here: https://github.com/vercel/next.js/tree/canary/examples/next-forms Maybe I am missing something, but the docs say Am I missing something here? Update:
|
But while deploying in vercel I will throw same error. Locally it runs despite of typescript error. |
Above to work locally and on vercel, have it like this:
|
Hi, the documentation needs to be updated in nextjs. They even did not mention this experimental stuff. Also i'm kinda start get frustrated these days. I'm currently in a migration to nextjs with my pet project and keep hit these kind of walls. Started with prisma and edge runtime. I know this is not easy and i'm really appriciate the work behind this meta framework but this server side action things needs to be clearified. Most of these kind of frameworks already can provide a single loading state from server side action. I think the workaround maybe the startTransition hook but i don't know because this is very under documented. Sorry for this but i'm overall kinda frustrated the current frontend era.... |
I see that this nextjs product have import { experimental_useFormStatus as useFormStatus } from "react-dom"; |
how was this problem solved im still very much confused |
I updated NextJS 13.5.5 to 13.5.7, it worked for me.
|
This does work in NextJS 13.5.6, but now seems to be broken in NextJS 14.0.0. Anyone else having this issue? |
Can you be more specific? What I shared above (#49232 (comment)) works for me. |
Ignore me... Apologies.... I was missing my morning coffee and not reading error responses... and on my second attempt see it's working. Our code for NextJS 13.5.6 works in NextJS 14.0.0, with the only change being to remove the 'experimental_' prefix within the imports (both for useFormState and useFormStatus). |
I just updated to next.js 14. I had to add // @ts-expect-error
import { useFormStatus } from 'react-dom'; else typescript throws an error that useFormStatus is not exported . It works fine for me now though. |
In my case the TS module resolution error was solved by adding |
refer to the NextJS latest example https://github.com/vercel/next.js/tree/canary/examples/next-forms |
Yes! See above: #49232 (comment) |
Maybe not the Not the "experimental" nor "canary" versions
But the "latest"
Or still better to use specific versions as in the official example
ammended with the type react-dom version I have mentioned above.
Cheers!
…On Fri, 3 Nov, 2023, 14:23 micheltucker, ***@***.***> wrote:
refer to the NextJS latest example
https://github.com/vercel/next.js/tree/canary/examples/next-forms in that
use correct the verion of react-dom in package.json to latest. This will
resolve the pnpm run build error ***@***.***/react-dom": "^18.2.14",
Yes! See above: #49232 (comment)
<#49232 (comment)>
—
Reply to this email directly, view it on GitHub
<#49232 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AQZ4KG2SQQRNCFPRXLHX2BDYCSWKLAVCNFSM6AAAAAAXWE2PJKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOJSGA3DQOBQGA>
.
You are receiving this because you commented.Message ID: <vercel/next.
***@***.***>
|
Started working for me after running |
I still cannot get rid of this error Any other ideas how to get rid of this error? |
Hi in my case i was using the useFormStatus in the same component as the form to disable the submit button when the for is submiting. and i got that issue: I created a separate component, lets called SubmitButton and i placed the useFormStatus and call it there, then i import the component and used as a child of the form, and then i used the ts annotation: The button component: `
} |
I just fixed it by changing versions of: "@types/react-dom": "18.2.17",
"@types/react": "18.2.39",
"next": "canary",
"eslint-config-next": "canary",
"react": "next",
"react-dom": "next", with the given values. I hope that works. |
@kuijpie thank you 🙏🏻 the only thing that helped me is to place action and form tag in a Server component, but the form content in JSX.fragment inside Client component. |
If you're using the next and react libraries with their experimental version, you should upgrade your |
I got the error For me "use client";
import { Button, ButtonProps } from "@mui/material";
import { useFormStatus } from "react-dom";
const FormButton = (props: ButtonProps) => {
const { pending } = useFormStatus();
return <Button disabled={pending} {...props} />;
};
export default FormButton; The above code does not give a TS error but gives the runtime error mentioned earlier. The reason is types and the implementations do not match. In implementation, it is still experimental. So I had to do this, "use client";
import { Button, ButtonProps } from "@mui/material";
// import { useFormStatus } from "react-dom";
// @ts-expect-error Types and implementations do not match
import { experimental_useFormStatus as useFormStatus } from "react-dom";
const FormButton = (props: ButtonProps) => {
const { pending } = useFormStatus();
return <Button disabled={pending} {...props} />;
};
export default FormButton; Now the runtime error goes away and in "dependencies": {
"next": "13.5.4",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@types/react": "^18",
"@types/react-dom": "^18"
} I have no idea how to do version matching here because they all have the same major release version. |
After upgrading to Next 14 (14.0.4 to be exact), I could import |
The only way useFormStatus work is As a hook, it can only live in a client component and can only be used as a child of a form element using a Server Action. So convert the button, input to a seperate client component and place the component inside a form ie. ''use server' customButton.tsx login.tsx |
We now have an example of forms + |
Verify canary release
Provide environment information
Which area(s) of Next.js are affected? (leave empty if unsure)
App directory (appDir: true)
Link to the code that reproduces this issue
https://github.com/rliang/next-use-form-status-bug-reproduction
To Reproduce
npm run dev
localhost:3000
in the browsersubmit
buttonDescribe the Bug
useFormStatus
doesn't work, such that its return value is never updated when submitting a form.Expected Behavior
The page should update the
formStatus
after clicking onsubmit
.Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
NEXT-1168
The text was updated successfully, but these errors were encountered: