最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

forms - TypeError: Cannot destructure property 'getFieldState' of '(0 , react_hook_form__WEBPACK_IMPORTE

programmeradmin1浏览0评论

I'm using Shadcn UI to implement a forgot-password form in my Next.js project, but I'm getting this error:

TypeError: Cannot destructure property 'getFieldState' of '(0 , react_hook_form__WEBPACK_IMPORTED_MODULE_4__.useFormContext)(...)' as it is null.

Here is my zod form:

const forgotPasswordFormSchema = z.object({
  email: z.string().email(),
});

const forgotPasswordForm = useForm<z.infer<typeof forgotPasswordFormSchema>>({
  resolver: zodResolver(forgotPasswordFormSchema),
  defaultValues: {
    email: "",
  },
});

And here is my Form html:

<Form {...forgotPasswordForm}>
  <form onSubmit={forgotPasswordForm.handleSubmit(handlePasswordReset)} className="space-y-5">
    <FormField
      control={forgotPasswordForm.control}
      name="email"
      render={({ field }) => (
        <FormItem>
          <FormLabel className="font-semibold" htmlFor="email">{messages.yourEmailLabel}</FormLabel>
          <FormControl>
            <Input id="email" type="email" {...field} />
          </FormControl>
          <FormMessage />
        </FormItem>
      )}
    />
  </form>
</Form>

My other Shadcn Form components in my app work just fine, so why is this one getting this error?

I'm using Shadcn UI to implement a forgot-password form in my Next.js project, but I'm getting this error:

TypeError: Cannot destructure property 'getFieldState' of '(0 , react_hook_form__WEBPACK_IMPORTED_MODULE_4__.useFormContext)(...)' as it is null.

Here is my zod form:

const forgotPasswordFormSchema = z.object({
  email: z.string().email(),
});

const forgotPasswordForm = useForm<z.infer<typeof forgotPasswordFormSchema>>({
  resolver: zodResolver(forgotPasswordFormSchema),
  defaultValues: {
    email: "",
  },
});

And here is my Form html:

<Form {...forgotPasswordForm}>
  <form onSubmit={forgotPasswordForm.handleSubmit(handlePasswordReset)} className="space-y-5">
    <FormField
      control={forgotPasswordForm.control}
      name="email"
      render={({ field }) => (
        <FormItem>
          <FormLabel className="font-semibold" htmlFor="email">{messages.yourEmailLabel}</FormLabel>
          <FormControl>
            <Input id="email" type="email" {...field} />
          </FormControl>
          <FormMessage />
        </FormItem>
      )}
    />
  </form>
</Form>

My other Shadcn Form components in my app work just fine, so why is this one getting this error?

Share Improve this question asked Feb 6 at 21:38 JasperanJasperan 3,7166 gold badges27 silver badges60 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It turns out I was importing the Form component from react-hook-form when it should have been imported from @/components/ui/form. Changing this import fixed the error.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论