嗨,我正在尝试用 React 制作一个表单,当我输入
ref={register}
inside
<input className="form-control" ref={register} type="text" name="text" id="text" />
我收到以下错误:
TypeError: path.split is not a function
get
src/utils/get.ts:6
有什么建议么?我正在跟进这家伙的 https://www.youtube.com/watch?v=bRRA-SrNyxg优酷教程:
这是我的 return() 语句上面的代码:
export const CreateTodo = () => {
const { register, handleSubmit} = useForm();
const onSubmit = handleSubmit((data) => {
alert(JSON.stringify(data));
});
return ( ... );
}
代码沙盒链接: https://codesandbox.io/s/pathsplit-error-2fwdt?file=/src/App.js
注册输入的方式在react-hook-form v7.0.0(您正在使用的版本)中发生了变化。
来自docs https://react-hook-form.com/migrate-v6-to-v7,
register
方法不再出现在ref
,而是调用函数本身并将 props 传播到输入中。该函数本身将返回以下道具:onChange
, onBlur
、姓名和ref
.
- <input ref={register({ required: true })} name="test" />
+ <input {...register('name', { required: true })} />
+ <TextInput {...register('name', { required: true })} />
<input
className="form-control"
{...register('text')}
type="text"
/>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)