0

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

enter image description here

1 Answer 1

2

You have to use:

this.setState({...})

instead of:

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

in order to trigger ui update.

const stockAdjustments = []

querySnapshot.forEach((doc) => {
    stockAdjustments.push({
        id: doc.id, ...doc.data()
    })
})
this.setState({ stockAdjustments });
Sign up to request clarification or add additional context in comments.

2 Comments

i have tried this but i am getting a Component Exception, "undefined is not a function(near '...this.state.stockAdjustments.map...') "
Ah i see, i hadnt made a temp array and was trying to set the state without, thanks

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.