Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

255
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda