简体   繁体   中英

Items not rendering in a FlatList

I'm trying to retrieve all the data I have in my firestore database but I can't seem to accomplish it. Been trying to do for the past couple days I had no luck. The data doesn't seem to be retrieving at all for some reason. I did a console log of my useState() variable which is where the data should be stored, but nothing is showing. I am also new to using React Native...

 const [evs, setData] = useState([]); const subscriber = firestore.collection('stations'); useEffect(() => { return subscriber.onSnapshot((querySnapshot) => { const list = []; querySnapshot.forEach(doc => { const {connectorID, connectorType} = doc.data(); list.push({ id: doc.id, connectorID, connectorType }); }); setData(list); }); }, []); console.log("LIST OF ITEMS: " + evs); return ( <View> <Text>Charging Points Available</Text> <SectionList data={evs} keyExtractor={(item) => item.id} renderItem={({item}) => <TextComp data={item.key}/>} /> </View> );

UPDATE, Still not working..

 const subscriber = firestore.collection('stations') .onSnapshot(querySnapshot => { const list = []; querySnapshot.forEach(documentSnapshot => { list.push({ ...documentSnapshot.data(), key: documentSnapshot.id, }); }); setData(list); setLoading(false); });

If your effect returns a function, React will run it when it is time to clean up

remove return from useEffect. and try

useEffect(() => 
  {
      subscriber.onSnapshot((querySnapshot) => {
          const list = [];
          querySnapshot.forEach(doc => {
            const {connectorID, connectorType} = doc.data();
            list.push({
              id: doc.id,
              connectorID,
              connectorType 
            });
          });
          setData(list);

      });
  }, []);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM