gpt4 book ai didi

react-hook-form - 获取未捕获的 TypeError : path. 拆分不是 react 中的功能

转载 作者:行者123 更新时间:2023-12-03 13:56:05 25 4
gpt4 key购买 nike

我正在尝试对我的表单进行验证以使用react。我选择了“react-hook-form”库。但我不断收到错误“Path.split 不是一个函数。即使在使用他们网站中给出的默认示例之后,我也遇到了同样的错误。
这是官方网站中给出的默认代码。

import React from "react";
import { useForm } from "react-hook-form";

export default function App() {
const { register, handleSubmit, watch, errors } = useForm();
const onSubmit = data => console.log(data);

console.log(watch("example")); // watch input value by passing the name of it

return (
{/* "handleSubmit" will validate your inputs before invoking "onSubmit" */}
<form onSubmit={handleSubmit(onSubmit)}>
{/* register your input into the hook by invoking the "register" function */}
<input name="example" defaultValue="test" ref={register} />

{/* include validation with required or other standard HTML validation rules */}
<input name="exampleRequired" ref={register({ required: true })} />
{/* errors will return when field validation fails */}
{errors.exampleRequired && <span>This field is required</span>}

<input type="submit" />
</form>
);
}

最佳答案

react-hook-form 从 6.X.X 更新到 7.0.0 并且有重大变化:
您必须更换所有 ref={register}{...register('value_name')} 示例:
版本 6.X.X:

<input ref={register({ required: true })} name="test" />
7.0.X 版:
<input {...register('test', { required: true })} />

关于react-hook-form - 获取未捕获的 TypeError : path. 拆分不是 react 中的功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66927051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com