我调用 api 来获取我的movieData
API:https ://obscure-reaches-65656.herokuapp.com/api/getCloseTime?city=Hsinchu&sTime=18&eTime=21
我认为数据是正确的SectionList
,数据是一个包含很多对象的数组,但我仍然得到错误:
Cannot read property 'length' of undefined
无法弄清楚如何解决数据问题?
这里是console.log(movieData);
:
这是关于我的类组件渲染:
render() {
const movieData = this.props.searchTime;
if (this.props.loading) {
return <Spinner text='Loading...' />;
}
console.log('movieData =>');
console.log(movieData);
return (
<View>
<SectionList
renderSectionHeader={this.sectionComp}
renderItem={this.renderSectionItem}
sections={movieData}
ItemSeparatorComponent={() => <View><Text></Text></View>}
ListHeaderComponent={() => <View style={{ backgroundColor: '#25B960', alignItems: 'center', height: 30 }}><Text style={{ fontSize: 18, color: '#ffffff' }}>Header</Text></View>}
ListFooterComponent={() => <View style={{ backgroundColor: '#25B960', alignItems: 'center', height: 30 }}><Text style={{ fontSize: 18, color: '#ffffff' }}>Footer</Text></View>}
/>
</View>
);
}
}
如果我设置一个测试数据testSections
而不是movieData
它的工作。
const testSections = [
{ key: "A", data: [{ title: "test1-1" }, { title: "test1-2" }, { title: "test1-3" }] },
{ key: "B", data: [{ title: "test2-1" }, { title: "test2-2" }, { title: "test2-3" }, { title: "test2-4" }, { title: "test2-5" }] },
{ key: "C", data: [{ title: "test3-2" }, { title: "test3-2" }] },
{ key: "W", data: [{ title: "test4-1" }, { title: "test4-2" }, { title: "test4-3" },{ title: "test4-4" }, { title: "test4-5" }, { title: "4-6" }] },
];
我的自定义渲染功能是这样的:
renderSectionItem = (info) => {
console.log('what is _renderItem info =>');
console.log(info);
const txt = ' ' + info.item.theaterCn;
return <Text
style={{ height: 60, textAlignVertical: 'center', backgroundColor: "#ffffff", color: '#5C5C5C', fontSize: 15 }}>{txt}</Text>
}
sectionComp = (info) => {
console.log('what is _sectionComp info =>');
console.log(info);
const txt = info.section.key;
return <Text
style={{ height: 50, textAlign: 'center', textAlignVertical: 'center', backgroundColor: '#9CEBBC', color: 'white', fontSize: 30 }}>{txt}</Text>
}
我不知道为什么。
任何帮助,将不胜感激。提前致谢。