javascript - ref={register} inside <input> is giving me a path.split error - Stack Overflow

Hi I'm trying to make a form with React and when I putref={register} inside<input className=&

Hi I'm trying to make a form with React and when I put

ref={register} inside

<input className="form-control" ref={register} type="text" name="text" id="text" />

I get the following error:

TypeError: path.split is not a function
get
src/utils/get.ts:6

Any suggestions? I'm following this guy's youtube tutorial:

Here is the code above my return () statement:

 export const CreateTodo = () => {
        const { register, handleSubmit} = useForm(); 
    
        const onSubmit = handleSubmit((data) => {
            alert(JSON.stringify(data));
        });

return ( ... );
}

CodeSandbox link:

Hi I'm trying to make a form with React and when I put

ref={register} inside

<input className="form-control" ref={register} type="text" name="text" id="text" />

I get the following error:

TypeError: path.split is not a function
get
src/utils/get.ts:6

Any suggestions? I'm following this guy's youtube tutorial:

Here is the code above my return () statement:

 export const CreateTodo = () => {
        const { register, handleSubmit} = useForm(); 
    
        const onSubmit = handleSubmit((data) => {
            alert(JSON.stringify(data));
        });

return ( ... );
}

CodeSandbox link:

Share Improve this question edited Apr 3, 2021 at 4:30 adnjoo asked Apr 3, 2021 at 4:28 adnjooadnjoo 1093 silver badges9 bronze badges 2
  • Can you share a minimal CodeSandbox that reproduces the issue? – Arun Kumar Mohan Commented Apr 3, 2021 at 4:30
  • @ArunKumarMohan ok I have shared a CodeSandbox link – adnjoo Commented Apr 3, 2021 at 4:33
Add a ment  | 

2 Answers 2

Reset to default 8

The way to register inputs has changed in react-hook-form v7.0.0 (the version you're using).

From the docs,

register method is no longer occurred at ref, instead invoke the function itself and spread the props into the input. The function itself will return the following props: onChange, onBlur, name and 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"
/>

According to new Version (7.x.x) You have to pass the register Like this

<input {...register('test', { required: true })} />

in 6.x.x it was like

<input inputref={register('test', { required: true })} />

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744210374a4563303.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信