简体   繁体   English

我在从数组 React Native 渲染数据时遇到问题

[英]I am having trouble rendering data from an array React Native

i can log the data but cant seem to display it, the screen is completely blank.我可以记录数据但似乎无法显示它,屏幕完全空白。 im not getting any errors but still nothing on the screen我没有收到任何错误,但屏幕上仍然没有任何内容

import React from 'react';
import { View, StyleSheet, Text } from 'react-native';
import firestore from '@react-native-firebase/firestore';
import { FlatList } from 'react-native-gesture-handler';

export default class StockAdjustmentScreen extends React.Component {
     constructor(props) {
          super(props)
          this.state = {
               stockAdjustments: [],
          }
     }
     componentDidMount() {
          firestore().collection('stockAdjustments')
               .get().then((querySnapshot) => {
                    querySnapshot.forEach((doc) => {
                         this.state.stockAdjustments.push({
                              id: doc.id, ...doc.data()
                         })
                    })
                    console.log(this.state.stockAdjustments)
               })
               .catch((error) => {
                    console.log
                         (error)
               })
     }
     render() {
          let Adjustments = this.state.stockAdjustments.map((val, key) => {
               return (
                    <View key={key}>
                         <FlatList style={styles.texts}>{val.comments}</FlatList>
                    </View>
               )
          })
          return (
               <View>
                    {Adjustments}
               </View>
          )
     }
}

here is the data i am trying to display这是我要显示的数据

在此处输入图片说明

You have to use:你必须使用:

this.setState({...})

instead of:代替:

this.state.stockAdjustments.push({...})

in order to trigger ui update.为了触发ui更新。

const stockAdjustments = []

querySnapshot.forEach((doc) => {
    stockAdjustments.push({
        id: doc.id, ...doc.data()
    })
})
this.setState({ stockAdjustments });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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