Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

305
Views
How to stop displaying error when data inside text input was set like this: value={value} React Hook Form

React Hook Forms detect that I change the value of text input when I type something (onChange). But can it also detect the change if I enter data to the input by value={value}?

const validator = register(name);

I need something like

onValueSet={(e) => {
            validator.onChange(e);
          }}

I mean if I just set data by value={value} I get an error that this input is required. I don't want this error, because data was set by value={value}.

Here is my input:

<StyledInput
          name={name}
          maxLength={100}
          value={value}
          onChange={(e) => {
            validator.onChange(e);
          }}
        />

and my validation schema:

const validationSchema = Yup.object().shape({
    first_name: Yup.string()
      .required("required"),
  });
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You have to use reset here and call it when you received your initial form data. I assume you're doing an api call and want to set the result to the form. You can do so with useEffect, watching when you're data has resolved and then reset the form with the actual values. This way you don't have to set the value via the value prop and let RHF manage the state of your inputs. You also don't need to set name prop, as RHF's register call returns this prop as well.

const Component = (props) => {
  const { result } = useApiCall();
  const { register, reset } = useForm();

  useEffect(() => {
    reset(result)
  }, [result]);

  return (
   ...
    <StyledInput
      {...register('first_name')}
      maxLength={100}
    />
    ...
  )
}

Here is a little CodeSandbox demonstrating your use case:

Edit React Hook Form - Reset Form (forked)

about 4 years ago · Juan Pablo Isaza Report

0

You can define an onfocus or onkeyup event and call the validation function there instead of onChange event.

<StyledInput
  name={name}
  maxLength={100}
  value={value}
  onfocus={(e) => {
    validator.onChange(e);
  }}
/>
about 4 years ago · Juan Pablo Isaza Report

0

Instead of triggering the validator when input changes, you can instead call your validator through the onBlur event. And I am not sure how you are using react-hook-form .. but the useForm hook has a config mode (onChange | onBlur | onSubmit | onTouched | all = 'onSubmit') on when to trigger the validation:

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!