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

264
Vistas
timing issue, console.log show Array element but length is zero

In FeedScreen, I try to get "userData" from redux store and feed it into a Flatlist. But nothing is shown. I check console.log with Chrom dev tool, I can see the element in "userData" but .length is zero and "userData[0]" is undefined.

If I refreshed the app with expo, the flatlist is shown.

I think it is related to timing as "userData" is fetched from firebase store and FeedScreen is nested in MainScreen Tab Navigator.

How to fix it?

the code shown below is extracted only from the relevant section, and I log the redux store in MainScreen.js. The redux reducer is working as expected.

FeedScreen.js

export default function FeedScreen(props,{ navigation }) {

    const [useData2, setUserData2] = useState({});

        const userData = useSelector(state=> state.userState.userdata)

        console.log(userData)
         console.log(userData.length)
}

MainSceen.js

....

  let tempPostArr2=[];
  let subUserPost = followingList.map(uid => {

    const q = query(collection(db, "post", uid, "userPosts"), orderBy("timestamp", "asc"));

    const unsubscribe = onSnapshot(q, (querySnapshot) => {

      let tempPosts = querySnapshot.docs.map((doc) => {

        const data = doc.data();
        const postId = doc.id;
        return { postId, ...data, uid }
      })
      tempPostArr2.push(tempPosts) 
    })
  })
  dispatch(fetchAllUsersPost(tempPostArr2))


  return (
    <Tab.Navigator initialRouteName='Feed' labeled={false}>
      <Tab.Screen name="Feed" component={FeedScreen}
        options={{
          tabBarLabel: 'Home',
          tabBarIcon: ({ color, size }) => (
            <MaterialCommunityIcons name="home" color={color} size={size} />
          ),
        }} />
    </Tab.Navigator>
  )
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Minor point about console.log: if you feed it an object directly like you've done, it tends to be "live" if you try to open it to look at object contents. So if you want to truly know what the object was at the time of logging, console.log(JSON.stringify(x)) is necessary.

about 4 years ago · Juan Pablo Isaza Denunciar

0

I found the answer, onSnapshot provides a callbackfunction. The dispatcher runs before the callback function runs. I make a state change in the callback function and use useEffect to relaunch the dispatcher.

reference

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