Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

306
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda