1

I filled an array with querySnapshot but flatlist doesn't render anything

Tried to change renderItem code

constructor(props) {
  super(props);
  var rootref = firebase.firestore().collection("deneme");
  var wholeData = []
  rootref.get().then(function(querySnapshot){
      querySnapshot.forEach(function(doc) {
        wholeData.push(doc.data())
      });
  });
};

render() {
  return (
    <View>
      <FlatList
         data={this.wholeData}
         renderItem={({item}) => <Text>{item.isim}, {item.soyisim}</Text>}
      />
    </View>
  );
};

1 Answer 1

2

You will have to use setState to notify the component that the data has changed. Change your code to do the following:

 constructor(props) {
      super(props);

      this.state = {
       data: []
      };

      var rootref = firebase.firestore().collection("deneme");
      rootref.get().then(querySnapshot => 
       const wholeData = querySnapshot.map(doc => doc.data()));

       // notify your component that the data has changed
       this.setState({
        data: wholeData
       })
    };

 render() {
  return (
    <View>
      <FlatList
         data={this.state.data} // get your data from the state
         renderItem={({item}) => <Text>{item.isim}, {item.soyisim}</Text>}
      />
    </View>
  );

This way as soon as you have received the wholeData, the FlatList will update.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but now im having another error querySnapshot is not a function.
querySnapshot is just an object. You should check your code if you are calling it as a function by mistake.
I copied the entire code. const part is giving an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.