Flatlist呈现来自Firestore的重复数组

时间:2018-09-03 22:40:20

标签: arrays react-native google-cloud-firestore react-native-flatlist

我正在将数据从Firestore拉到平面列表中。由于某种原因,我从firestore的数组呈现了两次。有人遇到过这个问题吗?

onCollectionUpdate = (querySnapshot) => {
var reviews = [];
let that = this;
querySnapshot.forEach((doc) => {
const { user, review, image} = doc.data();
reviews.push({
  key: doc.id,
  user: doc.data().user,
  review: doc.data().review, 
  image: doc.data().image, 
    });
  });
  that.setState({ 
  reviews,
  loading: false,
  });
}

我的渲染功能在下面。

_renderItem(reviews){
console.log(reviews);
<View style={{height: 60, marginRight: 0, marginLeft: 0, backgroundColor: '#D3D3D3', flexDirection: 'row'}}>
          <View style={{marginLeft: 10, justifyContent: 'center'}}>
              <Avatar
                small
                rounded
                source={{uri: reviews.item.image}}
                activeOpacity={0.7}
              />
          </View>
          <View style={{marginLeft: 5, justifyContent: 'center'}}>
              <Text>
              {reviews.item.user}
              </Text>
              <ReadMore
                  numberOfLines={1}
                  renderRevealedFooter={this._renderRevealedFooter}>
                  <Text>                       
                  {reviews.item.review}
                  </Text>
              </ReadMore>
          </View>
 </View>

  }

我的单位列表是

<FlatList 
  data={this.state.reviews}     
  keyExtractor={(item, index) => index}
  renderItem={this._renderItem.bind(this)}/> 

1 个答案:

答案 0 :(得分:0)

弄清楚了。我需要为评论创建一个变量,然后将其链接到this.state。当我创建一个空变量时,我被复制了。只需放下

var评论= this.state.reviews

解决了问题