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

259
Visualizações
How can I fix this problem in React Native Firebase?

I have the next problem:

export async function updateLessons() { 
  let data
  await database().goOnline().then(async () => {
    await database()
    .ref('days')
    .on('value', snapshot => {
      console.log(snapshot.val())
      data = snapshot.val();
    });
  });

  return data;
}

I use this function to update my application when I swipe down

  const onRefresh = React.useCallback(async () => {
    setRefreshing(true);
    setLessons(await updateLessons());
    console.log(lessons)
    setRefreshing(false);
  }, []);

It is called from scroll view (refreshcontrol)
The problem is that it doesn't work asynchronously. In console log i see my snapshot. But the application updated faster and in console.log(lessons) it is undefined. How can I fix it?

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

0

Updating state is an asynchronous operation, and won't have completed yet by the time your console.log statement runs.

A simple way to get the correct output, is to capture the new lessons in local variable (which is updated synchronously):

const onRefresh = React.useCallback(async () => {
  setRefreshing(true);
  const newLessons = await updateLessons();
  setLessons(newLessons);
  console.log(newLessons)
  setRefreshing(false);
}, []);

Also see:

  • The useState set method is not reflecting a change immediately, which also shows how to use a useEffect hook to respond to state changes.
  • React setState not updating state
  • Updated state values are not displaying in react
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