简体   繁体   English

将对象作为道具传递并在 React js 中对其使用 map() 函数

[英]Passing Object as a props and using map() function on it in react js

When I pass the object data as props to the component and looping through the data using the map() function I get the error TypeError: Cannot read property 'map' of undefined .当我将对象数据作为道具传递给组件并使用map()函数遍历数据时,我收到错误TypeError: Cannot read property 'map' of undefined

import React from 'react'
import styles from './social.module.css';
import {Button, Typography, CardContent, CardActions, Card} from '@material-ui/core'
const SocialItem = ({
    Icon,
    title,
    mixed,
    red,
    blue,
    item
}) => {
    return (
        <Card className={styles.socialItem}>
            <CardContent>
                <Typography className={styles.socialItem__typo} variant="h4" component="h2">{title}</Typography>
                <Icon
                    className={`${styles.socialItem__Logo} ${mixed && styles.mixedInsta} ${blue && styles.blueFacebook} ${red && styles.redInsta}`}/>
                <div className={styles.desc}>
                    {item.map(itm => <li>{itm.price}</li>)}
                </div>
            </CardContent>
            <CardActions className=""></CardActions>
        </Card>
    )
}

export default SocialItem

I haven't seen how the "item" looks like and supposing it's an actual array, you can try add this:我还没有看到“项目”的样子,假设它是一个实际的数组,你可以尝试添加:

{item !== undefined && item.map(itm => <li>{itm.price}</li>)}

If this doesn't work check what you pass to the component as the "item" prop.如果这不起作用,请检查您作为“项目”道具传递给组件的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM