使用React Hook Form时出现未捕获运行时错误导致页面空白的求助
使用React Hook Form时出现未捕获运行时错误导致页面空白的求助
看起来你遇到的问题大概率是代码里的拼写错误导致的!我帮你梳理一下核心问题点:
- 最关键的错误:你把
handleSubmit拼写成了handelSubmit(少了一个字母d)!这个拼写错误会让React找不到这个从useForm解构出来的方法,直接抛出未捕获的运行时错误,这就是页面空白的根源。 - 另外还有个不影响功能的小笔误:
<h1>Helllo</h1>多打了一个l,改成<h1>Hello</h1>就规范了。
下面是修正后的完整代码:
import { useForm } from "react-hook-form"; export const Form = () => { // 修正handleSubmit的拼写 const { register, handleSubmit } = useForm(); const onSubmit = (data) => { console.log(data); }; return ( <div> {/* 修正标题的拼写 */} <h1>Hello</h1> <form onSubmit={handleSubmit(onSubmit)}> <input type="text" placeholder="Full Name..." {...register("FullName")}/> <br/> <input type="text" placeholder="Email..." {...register("Email")}/> <br/> <input type="text" placeholder="Age..." {...register("Age")}/> <br/> <input type="password" placeholder="Password..." {...register("password")}/> <br/> <input type="password" placeholder="Confirm Password..." {...register("ConfirmPassword")}/> <br/> <input type="submit"/> </form> </div> ); };
最后给你个小建议:下次遇到控制台的未捕获错误时,一定要仔细阅读错误信息,通常它会明确指出是哪个变量/方法未定义,顺着这个提示就能快速定位问题啦~这类拼写错误在开发中很常见,多留意就能避免!
备注:内容来源于stack exchange,提问作者Abhinay Challa
相关产品推荐
相关产品推荐

