如何在FlatList(React Native)中从Firebase实时检索数据?

时间:2018-07-17 06:32:10

标签: firebase react-native firebase-realtime-database

我无法在FlatList中显示在注释中检索到的数据。 有什么可能的解决方案,以便我可以显示UsersData中的所有数据并将其显示在我的render方法的FlatList中?

Firebase数据库

enter image description here

const comments = [
      {reply: 'Reply 1'},
      {reply: 'Reply 2'},
      {reply: 'Reply 3'}];
export default class App extends Component {

  constructor(props) {

    super(props);

    this.state = {
      comments: comments
    }
  }

componentDidMount() {

    var that = this

    firebase.database().ref('/UsersData').on('child_added', function (data) {

      var newData = [...that.state.comments]
      newData.push(data)
      that.setState({ comments: newData })

    })

  }

  render() {
    return (
      <View style={styles.container}>
        <FlatList
          contentContainerStyle={styles.paragraph}
          data={this.state.comments}//The Data is not passed properly  
          renderItem={({data}) => <Text>{data.val().name}</Text>}
               //It should be pas displayed here
         />
      </View>
    );
  }
}

consloe.log(newData)返回 console

0 个答案:

没有答案