React native 无法读取属性';长度';使用SectionList时未定义的
我调用api来获取我的React native 无法读取属性';长度';使用SectionList时未定义的,react-native,react-native-sectionlist,React Native,React Native Sectionlist,我调用api来获取我的movieDataapi: 我认为部分列表的数据是正确的,数据是一个包含大量对象的数组,但我仍然得到错误: 无法读取未定义的属性“length” 不知道如何解决数据问题 这里是console.log(movieData): 以下是有关我的类组件渲染的信息: render() { const movieData = this.props.searchTime; if (this.props.loading) { return <Spinn
movieData
api:
我认为部分列表的数据是正确的,数据是一个包含大量对象的数组,但我仍然得到错误:
无法读取未定义的属性“length”
不知道如何解决数据问题
这里是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>
);
}
}
我的自定义渲染函数如下所示:
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>
}
renderSectionItem=(info)=>{
log('what'u renderItem info=>');
控制台日志(信息);
const txt=''+info.item.theaterCn;
返回{txt}
}
sectionComp=(信息)=>{
log('what'u sectionComp info=>');
控制台日志(信息);
const txt=info.section.key;
返回{txt}
}
我不知道为什么
任何帮助都将不胜感激。提前感谢。数据结构movieData
不正确,SectionList
需要一个对象数组,每个对象都应该有标题
和数据
属性,请查看文档。因此,根据您的movieData
,您需要将值转换为该结构,如
样本
首先,将获取的数据转换为适当的结构
fetch('https://obscure-reaches-65656.herokuapp.com/api/getCloseTime?city=Hsinchu&sTime=18&eTime=21')
.then(r => r.json())
.then(r => {
const movieData = r.reduce((r,s) => {
r.push({title: s.theater, data: s.movie});
return r;
}, []);
this.setState({ movieData });
});
转换数据后,只需将其传递到SectionList
<SectionList
renderItem={({item, index, section}) => <Text key={index}>{item.enName}</Text>}
renderSectionHeader={({section: {title}}) => (
<Text style={{fontWeight: 'bold'}}>{title}</Text>
)}
sections={this.state.movieData}
keyExtractor={(item, index) => item.enName + index}
/>
{item.enName}
renderSectionHeader={({section:{title}}})=>(
{title}
)}
节={this.state.movieData}
keyExtractor={(项,索引)=>item.enName+index}
/>
希望这会有帮助 我遇到了同样的错误:
无法从deltaPatcher.js读取未定义长度的属性
我的解决方案是使用React本机调试器而不是浏览器控制台。
把它扔了,也许会对别人有帮助 您使用的是movieData.length
还是movieData.length()
?不,我只是将movieData
放入部分。我的movieData
结构就像我上传的照片一样。我不知道首先应该有标题和数据。你帮我省了很多天。非常感谢你。
<SectionList
renderItem={({item, index, section}) => <Text key={index}>{item.enName}</Text>}
renderSectionHeader={({section: {title}}) => (
<Text style={{fontWeight: 'bold'}}>{title}</Text>
)}
sections={this.state.movieData}
keyExtractor={(item, index) => item.enName + index}
/>