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

91
Visualizações
React Native, add two return statements to useEffect hook

How can I add two return statements to my useEffect hook? I would like to add an event listener to my current hook.

Event listener:

const subscription = AppState.addEventListener("change", nextAppState => {
  if (
    appState.current.match(/inactive|background/) &&
    nextAppState === "active"
  ) {
    console.log("App has come to the foreground!");
  }

  appState.current = nextAppState;
  setAppStateVisible(appState.current);
  console.log("AppState", appState.current);
});

return () => {
  subscription.remove();
};

Current hook:

useEffect(() => {

    async function checkRefresh() {
        if (
            daysDiffToNow(lastUpdatedTimestamp) > 0 &&
            appState.current.match(/inactive|background/) &&
            nextAppState === "active"
        ) {
            await onRefreshAppData();
        }
    }
    checkRefresh();

    const updateLastUpdatedTextCallback = (value) => {
        setState((prevState) => ({
            ...
        }));
    };
    const id = setInterval(() => {
        updateLastUpdatedTextCallback(lastUpdatedTimestamp);
    }, TIME_INTERVAL_IN_MILISECONDS);
    return () => clearInterval(id);
}, [lastUpdatedTimestamp]);

const close = () => {
    setState((prevState) => ({
        ...
    }));
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could simply put them top to bottom inside the useEffect and have a return function that would look like this (it executes at once the code for the two return statements needed) :

 return () => {
          subscription.remove();  // the first
          clearInterval(id);     // the second
        } 
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