简体   繁体   English

React-Native:TypeError:undefined 不是对象(评估“_this.props.item”)

[英]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.namethis.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.

相关问题 反应:this.props.item 未定义 - React: this.props.item is undefined undefined 不是 object(评估 'this.props = props') - undefined is not an object (evaluating 'this.props = props') React-Native expo 错误:TypeError: undefined is not an object(评估'this.props')。 react-native expo - error: TypeError: undefined is not an object (evaluating'this.props'). react-native expo React-native:undefined不是对象(评估&#39;_this3.props.navigation.navigate&#39;) - React-native: undefined is not an object (evaluating '_this3.props.navigation.navigate') 【React-native】undefined不是对象(评估&#39;_this.props.navigation.navigate&#39;) - 【React-native】undefined is not an object (evaluating '_this.props.navigation.navigate') React Native:TypeError:undefined 不是一个对象(评估&#39;_this.props.data.map&#39;) - React Native: TypeError: undefined is not an object (evaluating '_this.props.data.map') TypeError:undefined 不是 React Native 中的对象(评估“_this.props.navigation”) - TypeError: undefined is not an object (evaluating '_this.props.navigation') in React Native TypeError:undefined不是对象(评估&#39;this.props&#39;) - React Native - TypeError: undefined is not an object (evaluating 'this.props') - React Native TypeError: undefined is not an object (evalating '_this.props') in my react native project - TypeError: undefined is not an object (evaluating '_this.props') in my react native project react-native:TypeError:undefined 不是 object(正在评估“this.state”) - react-native : TypeError:undefined is not an object (evaluating 'this.state')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM