我正在尝试获取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有点类似:)
答案 0 :(得分:2)
您自然没有dataProvider
状态值。
this.state
是设置初始值:this.state={ dataProvider: data }
或state={ dataProvider: data }
dataProvider
的{{1}}类型为RecyclerListView
。
所以你可以尝试
dataProvider