Home > Blockchain >  react hook form clears input field after I enter field name
react hook form clears input field after I enter field name

Time:11-15

I'm using react-hook-form to validate text input field.

  const {
    register,
    handleSubmit,
    setValue,
    formState: { errors },
  } = useForm({ mode: 'onBlur' });

<input
  name='name'
  type='text'
  onInput={(e) => setValue(e.target.value)}
  {...register('name',{ required: true })}
/>

My problem is that when I enter text 'name' in that input - text clears. Also I have another input with name blog.

<input
  name='blog'
  type='text'
  onInput={(e) => setValue(e.target.value)}
  {...register('blog',{ required: true })}
/>

Now if I write blog - blog input clears. Funny thing is that you can write name inside blog input and text in name input clears. Same way If I write blog inside name input - blog input text clears.

I guess I'm making some dumb mistake, what am I doing wrong ? :)

CodePudding user response:

Need to specify the name when setting value

onInput={(e) => setValue("blog", e.target.value)}

CodePudding user response:

You were going right bro just a tiny mistake in your code.

const [blogValue,setBlogValue] = useState("");

<input
  name='blog'
  type='text'
  value ={blogValue}
  onChange={(e) => setBlogValue(e.target.value)}
/>

I hope your problem is solved. Best of Luck :)

  • Related