如何解决 TypeError: path.split is not a function

Yeu*_*mmy 4 typeerror reactjs react-hook-form

现在我使用react-hook-form进行登录验证。

但是,当在 input 标签中输入ref={register}时,TypeError: path.split is not a function错误会继续发生 。

import React from 'react';
import {useForm} from "react-hook-form";
import './Auth.css';

export default function Register() {

    const {register, errors, watch} = useForm();

    return (
        <div>
            <form>
                <label>Email</label>
                <input type="email" name="email" ref={register({ required: true})} />
                <label>Password</label>
                <input type="password" />
                <label>Password Confirm</label>
                <input type="password"/>
                <input type="submit" />
            </form>
        </div>
    );
}

Run Code Online (Sandbox Code Playgroud)

即使我复制并粘贴了示例代码,也会出现同样的错误,我该如何解决呢?

错误代码如下。 在此处输入图片说明

Jor*_*ris 9

我认为您使用的是带有 v6 语法的 React Hook Form v7,这就是您收到该错误的原因。

这是一个类似的问题:https : //github.com/react-hook-form/react-hook-form/issues/4595

使用 v7,您必须register像这样使用:

<input type="email" {...register('email', { required: true })} />
Run Code Online (Sandbox Code Playgroud)

或者安装 v6,这里的文档:https : //react-hook-form.com/v6/api#register