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

176
Visualizações
React native onChangeText execute only one function

I have a weird problem with react native 0.62, in onChangeText, I try to call 2 functions setData and checkEmailSyntax. I don't know why but only the first function executes. I have seen similar questions, but nothing works.

Also, I have this same problem with context. Context returns function like logout, setUser, and so on when I try to execute these functions one after another in useEffect only the last function is called.

Do you think this is a react native version problem? or it is my fault.

Ok, @Nestoro solved this mistary, the checkEmail is coping old state without a new email so the email state is empty. New question is can I mutate a wrongEmailSyntax state? or I have to use a useEffect and fire it every time when email changes?

      <TextInput
        style={styles.inputText}
        placeholder="e-mail"
        autoCompleteType="email"
        onChangeText={text => {
          setData({ ...data, input_email: text });
          checkEmailSyntax(text);
        }}
        autoCapitalize="none"
        placeholderTextColor="grey"
      />

  const checkEmailSyntax = (inputVal) => {
    if (inputVal.includes('@') && inputVal.includes('.')) {
      setData({ ...data, wrongEmailSyntax: false });
    } else {
      setData({ ...data, wrongEmailSyntax: true });
    }
  };

  const [data, setData] = useState({
    input_email: '',
    input_password: '',
    wrongEmailSyntax: false,
  });

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

In react updating state is asynchronous. If we try to consecutively update the state of two different keys of the same object by referencing the seemingly previously updated state in the following way, we will end up with an incomplete update.

const [inputs, setInputs] = useState({
    firstName: "",
    lastName: ""
})

const handleClick = () => {
    setInputs({ ...inputs, firstName: "John" })
    setInputs({ ...inputs, lastName: "Wayne" })
}

>> console.log(inputs)
>> { firstName: "", lastName: "Wayne" }

Solution is to pass a function which will hold the previous state present before the batched update -

setData(prevState => ({ ...prevState, input_email: text }));
...
setData(prevState => ({ ...prevState, wrongEmailSyntax: false }));
about 4 years ago · Juan Pablo Isaza Relatório

0

in OnChangeText call only the checkEmailSyntax function. And inside the if in checkEmailSyntax do this

setData({...data, wrongEmailSyntax: false, input_email: inputVal})

As A G describe state is async!

The new code

 <TextInput
        style={styles.inputText}
        placeholder="e-mail"
        autoCompleteType="email"
        onChangeText={text => {
          checkEmailSyntax(text);
        }}
        autoCapitalize="none"
        placeholderTextColor="grey"
      />
 const checkEmailSyntax = (inputVal) => {
    if (inputVal.includes('@') && inputVal.includes('.')) {
      setData({ ...data, wrongEmailSyntax: false, input_email: inputVal});
    } else {
      setData({ ...data, wrongEmailSyntax: true, input_email: inputVal });
    }
  };
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