• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

235
Vistas
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 3 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

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 3 years ago · Juan Pablo Isaza Denunciar

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 3 years ago · Juan Pablo Isaza Denunciar

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 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda