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

269
Visualizações
How to setState inside an onPress Alert function in React-Native

I'm currently playing with ways to change state within an onPress event in React-Native and haven't had much luck. When somebody presses the "Change State?" option in the summoned alert, I would want to change the state to true . However, when calling setState() or a useCallback() which toggles the state, it continues to print false printed (twice).

FYI: I call the alert as ExampleAlert() rather than <ExampleAlert /> elsewhere in my code.

Does this issue have something to do with the way the Alerts are written in RN, or the fact that Alerts do not trigger a re-render?

  const [state, setState] = useState(false);

  const changeStateCallback = useCallback(() => {
    setState(!state);
  }, [state]);

  const ExampleAlert = () => {
    Alert.alert(
      'Title',
      'Message',
      [
        {
          text: 'Change State?',
          onPress: () => {
            changeStateCallback;
            console.log(state);
            setState(true);
            console.log(state);
          },
        },
        {
          text: 'OK',
          onPress: () => {},
        },
      ],
    );
    return null;
  };
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

useState is async, you can not see the change in the same enviroment

so you just need do this:

onPress: () => {
            setState(true);
          },

or

const handlePress =()=>{
 setState(true);
}

    onPress: () => { handlePress },

this syntax () => { } does not execute yet, it's executed when the user clicked over that.

edit 1:

const handlePress =()=>{
 setState(true);
 console.log(state) // here you can not see the change
}

console.log(state) //put your console outside of handlePress to see the change
about 4 years ago · Juan Pablo Isaza Relatório

0

I think I know your problem.

console.log() is a synchronous function, setState() is an async one, called async code won't run until all what's in the current call stack finishes running.

You won't see changes in state until later on.

Have you tried visualizing state with debugging tools?

Similar question: Console.log() after setState() doesn't return the updated state

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