94

I'm trying to do validations for my form in react. I chose "react-hook-form" library. But I'm constantly getting error "Path.split is not a function. Even after using the default example given in their website, I'm getting the same error. This is the default code given in the official site.

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>
  );
}
4
  • Can you share a minimal CodeSandbox that reproduces the issue? Commented Apr 3, 2021 at 4:02
  • Hi @ArunKumarMohan Here is the link "codesandbox.io/live/ljesmy8" Commented Apr 3, 2021 at 4:58
  • 2
    Looks like you've shared a session URL instead of a CodeSandbox URL. I just answered a similar question here that should fix the issue. Replace ref={register} with {...register('example')}. Commented Apr 3, 2021 at 5:10
  • Yeah its working. Thanks @ArunKumarMohan. I didn't see migration docs. Commented Apr 3, 2021 at 5:17

6 Answers 6

194

react-hook-form updated to 7.0.0 from 6.X.X and has breaking changes:

You have to replace all ref={register} with {...register('value_name')}

Example:

Version 6.X.X:

<input ref={register({ required: true })} name="test" />

Version 7.0.X:

<input {...register('test', { required: true })} />
Sign up to request clarification or add additional context in comments.

6 Comments

You could run the codemon npx @hookform/codemod v7/update-register
@Ethaan how do you execute the command with yarn?
@Twirlman the npx command will still work even if you are using yarn.
yea as @BraydenW says, npx come with npm 5.2+
let suppose we have any reusable input field them how we will pass this register to that reusable function
|
28

Simple input with required and errors.message features, necessary changes in update:

From version 6.x.x:

function MyComponent(props) {
  const { register, handleSubmit, errors } = useForm();

  const onSubmit = (values) => {...};

  return (
    <div>
      <form onSubmit={handleSubmit(onSubmit)}>
        <input
          name="message"
          autoComplete="off"
          ref={register({
            required: "Required",
          })}
        />
        {errors.message && errors.message.message}
        <input type="submit" />
      </form>
    </div>
  );
}

To version 7.x.x:

function MyComponent(props) {
  const { register, handleSubmit, formState: { errors }} = useForm();

  const onSubmit = (values) => {...};

  return (
    <div>
      <form onSubmit={handleSubmit(onSubmit)}>
        <input
          name="message"
          autoComplete="off"
          {...register("message", {
            required: "Required",
          })}
        />
        {errors.message && errors.message.message}
        <input type="submit" />
      </form>
    </div>
  );
}

In addition to register fix, if you use errors from useForm(), now errors feature is exported from formState.

Comments

19

Worth mentioning that if you are using material ui or something similar, where ref={ref} throws an error (because it expects a different prop name instead of ref), you might want to

import { TextField } from '@material-ui/core';


return (
 <TextField {...register('name')} />
)

there is a migration guide for this here, but still worth to mention

Comments

2

As noted above, there are changes to how register is to be used in v7

If you are still getting errors, ensure that id is actually a string and not any other type such as a number.

   <input {...register(id)} />

Comments

0
import { useForm } from "react-hook-form";
export default function App() {
const { register, formState: { errors }, handleSubmit } = useForm();

return (
  <form onSubmit={handleSubmit(onSubmit)}>
   <input {...register("firstName", { required: true })} />
   {errors.firstName?.type === 'required' && "First name is required"}
  
   <input {...register("lastName", { required: true })} />
   {errors.lastName && "Last name is required"}
  
   <input type="submit" />
  </form>
 );
 }

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
this is giving errors
0

In react this kind of error usually occurs when we forgot to pass the field on which we need to perform the operation

Wrong ❌: setValue(filteredOptions)

Right ✔ : setValue('options',filteredOptions)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.