[英]React-Native: TypeError: undefined is not an object (evaluating '_this.props.item')
I'm a react-native beginner, I'm trying to render a FlatList with custom components for each item, when I get FlatList data array from backend I get the following error:我是一个 react-native 初学者,我试图为每个项目渲染一个带有自定义组件的 FlatList,当我从后端获取 FlatList 数据数组时,我收到以下错误:
TypeError: undefined is not an object (evaluating '_this.props.item')
Here is the code for my flatlist:这是我的平面列表的代码:
return(
<FlatList data={venueStore.venues}
renderItem={({ item }) => <ItemCard1 item={item}></ItemCard1>}
style={{ flex:1 }} keyExtractor = { (item, index) => item.name+index.toString() }
/>
);
My custom card component just in case:我的自定义卡片组件以防万一:
<View style={{ width:'100%', height:200, flexDirection:'column', position:'relative' }}>
<Image style={{ width:'100%', height:200, position:'absolute', top:0, left:0}} resizeMode="cover" source={{uri: this.props.item.image }}/>
<View style={{ width:'100%', height:200, backgroundColor:'rgba(0,0,0,0)', position:'absolute', top:0, left:0 }}></View>
<LinearGradient colors={[ 'rgba(0,0,0,0)', 'rgba(0,0,0,0.3)']} style={{position: 'absolute', left: 0, right: 0, top: 0, height: 200}}/>
<View style={{ flexDirection:'row' , borderRadius:2, padding:5, backgroundColor:'rgb(255,52,89)', alignItems:'center', position:'absolute', top:10, left:10 }}>
<Text style={{ fontSize:15, color:'white' }}>{this.props.item.id}</Text>
</View>
<TouchableHighlight onPress={toggleFavorited} style={{ justifyContent:'center', alignItems:'center', position:'absolute', top:10, right:10 }}>
{this.props.item.is_favorited
?<Icon name="heart" size={25} color="white" style={{ alignSelf:'center' }}/>
:<Icon name="heart-o" size={25} color="white" style={{ alignSelf:'center' }}/>}
</TouchableHighlight>
<View style={{ width:'100%', flexDirection:'row' , padding:5, alignItems:'center', position:'absolute', bottom:0, left:0 }}>
<Image style={{ width:50, height:50, borderRadius:50/2, marginRight:15, transform: [{ translateY: 0 }] }} resizeMode="cover" source={require('@assets/images/amaro-1.jpeg')}/>
<Text style={{ fontSize:20, color:'white' }}>{ props.name }</Text>
</View>
</View>
Why am I getting this error, I'm fairly certain venueStore.venues is an array of data, maybe the error is how I'm passing the props to ItemCard1, or maybe how I access the props via this inside ItemCard1?为什么我会收到这个错误,我相当肯定venueStore.venues 是一个数据数组,也许错误是我如何将道具传递给 ItemCard1,或者我如何通过它在 ItemCard1 中访问道具?
If your custom card component ItemCard1
is a functional component try to access props as shown below.如果您的自定义卡片组件
ItemCard1
是一个功能组件,请尝试访问如下所示的道具。 Also, in your custom component, you mixed props
eg props.name
and this.props
, you might want to correct that to the right one for all此外,在您的自定义组件中,您混合了
props
例如props.name
和this.props
,您可能希望将其更正为所有人
const ItemCard1 = (props) => {
//rest of component
<Text style={{ fontSize:15, color:'white' }}>{props.item.id}</Text>
}
For a class component it should be this.props
like in your question.对于类组件,它应该是
this.props
就像你的问题一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.