-
Notifications
You must be signed in to change notification settings - Fork 111
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
Issue with Next.js and Autoform: Use of FormProvider in Server Components #128
Comments
Can you expand why it crashes? I use Nextjs with AutoForm (version from month ago but this was also based on react-hook-form) and it generally works with Nextjs 13, 14, and 15. The only issue I found was that because AutoForm does Zod validation on client side, if you wish to re-use the same Zod schema on the server-side you need to create a copy and have one schema with "use client" and another without it (for server side). Aide from that (very annoying) issue - I didn't experience any other issues with Nextjs and AutoForm. |
I'm not sure I can explain it in more detail — I encountered the error on my first attempt, and resolved it by adding It seems clear to me that the form should be handled on the client side since it uses a context. You’re likely not experiencing the issue because your page or component calling AutoForm may be already marked as After analyzing it further, I found that when using |
It also doesn't work in Remix at all for probably the same reason. |
As described at https://autoform.vantezzen.io/docs/react/getting-started#nextjs-and-rsc, AutoForm is intended to be used in Client-side component since there had been problems with serializing zod schemas otherwise and handling of callbacks. I'll see if I can maybe mark AutoForm itself with "use client" or if serialization problems have been fixed since then. |
When using Next.js, integrating Autoform causes crashes due to the import of
FormProvider
(fromreact-hook-form
) within a server component.Temporary Solution
I had to manually add
"use client"
at the top ofautoform/utils.ts
andautoform/AutoForm.tsx
files to resolve the issue.Explanation
Next.js defaults to server components, but contexts like
FormProvider
cannot be used in server components. Changing these components to client components by adding"use client"
fixed the issue.The text was updated successfully, but these errors were encountered: