首页 > 解决方案 > 如何在 FlatList(React Native)中从 Firebase 实时检索数据?

问题描述

我无法显示在 FlatList 的评论中检索到的数据。什么是可能的解决方案,以便我可以显示来自 UsersData 的所有数据并将其显示在我的渲染方法的 FlatList 中?

Firebase 数据库

在此处输入图像描述

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) 返回 安慰

标签: firebasereact-nativefirebase-realtime-database

解决方案


推荐阅读