ReactNative从Firebase检索数据到RecyclerListView

时间:2019-09-06 15:38:47

标签: reactjs firebase react-native

我正在尝试获取RecyclerListView来显示我从firebase获得的数据。

但是我得到了错误

  

未定义不是对象(评估this.state.dataProvider.cloneWithRows)

这是我的代码

        constructor(props) {
          super(props)
          const categories = [];
          firebase.database().ref('category/' + this.props.navigation.getParam('category')).orderByChild('ratingCount').on('value', (snap) => {    
             snap.forEach((child) => {
              categories.push({
                type: 'NORMAL',
                item:{
                 id: child.val().id,
                 name: child.val().name,
                 description: child.val().rating,
                }
             });
             });

          alert(JSON.stringify(categories));

             this.setState({
               list: this.state.dataProvider.cloneWithRows(categories)
             });
           });

          this.state = { 
             list: new DataProvider((r1, r2) => {
                return r1 !== r2;
              })
          };

          this.layoutProvider = new LayoutProvider((i) => {
             return this.state.list.getDataForIndex(i).type;
           }, (type, dim) => {
             switch (type) {
               case 'NORMAL': 
                 dim.width = SCREEN_WIDTH;
                 dim.height = 100;
                 break;
               default: 
                 dim.width = 0;
                 dim.height = 0;
                 break;
             };
           })
        }

        rowRenderer = (type, data) => {
          const { name, description } = data.item;
          //i know im not displaying anydata here will when it work
        }
  render() {
        return (                                                                                                           
                <RecyclerListView
                style={{flex: 1, minHeight: 1, minWidth: 1,}}
             rowRenderer={this.rowRenderer}
             dataProvider={this.state.list}
             layoutProvider={this.layoutProvider}
                />     
       );                                                                                                                 
    }

这是Alert(JSON.stringify(categories))的作用

  

[{“ type”:“ NORMAL”,“ item”:{“ id”:0,“ name”:“ Mahmoud Photography”,“ description”:4}},{“ type”:“ NORMAL”, “ item”:{“ id”:1,“ name”:“ Ahmed Photography”,“ description”:5}}]

我在做什么错?如果有人可以帮助的话,RecyclerListView软件包与ListView有点类似:)

1 个答案:

答案 0 :(得分:2)

您自然没有dataProvider状态值。

this.state是设置初始值:this.state={ dataProvider: data }state={ dataProvider: data }

dataProvider的{​​{1}}类型为RecyclerListView

所以你可以尝试

dataProvider