Closed maxentr closed 1 year ago
Ooof, that's a ton of plain code. Do you mind posting a reproduction sandbox instead? It can help us debug the issue a lot faster.
..or at least the reproduction steps 😉
Alright, from what I've gathered, it appears that your app is crashing due to a Zod exception. The resolver in your application is throwing the following error:
[
{
"code": "invalid_type",
"expected": "string",
"received": "undefined",
"path": ["username"],
"message": "Required"
}
]
This exception occurs because the username
input field does not have a default value and is therefore undefined
which doesn't pass your from Zod validation. I assume you attempted to submit the form without modifying the input values. I recommend providing a default value for the input, such as an empty string.
That said, I'll look into why it's affecting your whole app by yielding an uncaught exception.
Maybe not, actually.. K, a repro is needed 😋
Yeah my bad for the code I didn’t think about a repro sandbox in first. Here it is: https://codesandbox.io/p/sandbox/thirsty-http-kbgkx6?file=%2FREADME.md%3A14%2C1.
@Maxentr I don't have access to codesandbox right now but could it be related to this? https://github.com/shadcn/ui/issues/410#issuecomment-1556705052
Can confirm the issue @shadcn @Maxentr and yes, the issue was resolve once I provided default values to the columns.
I updated the codesandbox with the default values. The crash is still there. By the way the crash is more like the tab freeze and make the whole browser lag
I'm still trying to get a hold of the issue, but it doesn't seem like it's input related. At least, I can reproduce it by commenting the whole FormField
component out of the form and refreshing the page: the same "frozen page" page effect.
As soon as I introduce the shadcn/ui Form
component, the page freezes 🧐
Can it be something React Context related in the context of RSC? I saw multiple thread about it on Twitter, but never bothered to look at it in detail
K, so, I think I got it (kinda).
I'm pretty sure it's React & RSC related and potentially something to do with how Next.js treats page.js
files (routes).
Extracting the form into it's own ProfileForm.js
with "use client"
directive doesn't yield the same infinite re-renders behaviour and works as expected.
page.js
import { ProfileForm } from './profile-form'
export default async function Page() {
return (
<div className='p-16 flex-1 flex flex-col'>
<h2 className='text-2xl font-bold mb-4'>Form</h2>
<div className='flex-1 flex flex-col gap-2'>
<ProfileForm />
</div>
</div>
)
}
profile-form.js
"use client"
const formSchema = z.object({
username: z.string().min(2, {
message: 'Name must be at least 2 characters.'
})
})
export function ProfileForm() {
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
username: ''
}
})
return (
<Form {...form}>
<form onSubmit={form.handleSubmit((data) => console.log(data))}>
<FormField
control={form.control}
name='username'
render={({ field }) => (
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input placeholder='username' {...field} />
</FormControl>
<FormDescription>This is your public display name.</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<Button type='submit'>Submit</Button>
</form>
</Form>
)
}
P.S. .js
extension is here to simply point out that it's a different file. You can write your .ts
or .tsx
files instead.
Here a working codesandbox fork of the OG codesandbox provided by @Maxentr
K, so, I think I got it (kinda).
I'm pretty sure it's React & RSC related and potentially something to do with how Next.js treats
page.js
files (routes).Extracting the form into it's own
ProfileForm.js
with"use client"
directive doesn't yield the same infinite re-renders behaviour and works as expected.
Thank you for finding the error. From what I understand I should avoid using "use client" in the page.tsx file by default.
Hello,
I tried to use the new react hook form with the example given in the doc but the app crashed.
Here is the code used in the app:
The form.tsx file:
My package.json's dependencies:
After investigating, I found that the
<FormItem>
component had ref a null. I don't know if it's me who missed something or if it's a real bug but in case it is a bug I prefer to tell.