I'm using the Shadcn Form component for my application. I have a simple form and schema:
const formSchema = z.object({
username: z.string(),
})
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
username: "",
},
});
And here's my form in the HTML:
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>User Name</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit">Submit</Button>
</form>
</Form>
However when I open up this form in my application, I receive the following error:
Uncaught TypeError: Cannot destructure property 'getFieldState' of 'useFormContext(...)' as it is null.
How do I fix this error?
This error was happening because I was importing Form from react-hook-form
instead of from the shadcn import @/components/ui/form
. Correcting the import fixed the error