简体   繁体   English

将 api 调用的道具发送到另一个反应组件时出现问题

[英]Having issues sending props from a api call into another react component

Ok so I want to make a API call to my GraphQL backend to get an array of campaigns which I want to display on the React frontend in the form of a card.好的,所以我想对我的 GraphQL 后端进行 API 调用,以获取我想以卡片形式在 React 前端显示的一系列活动。 When I make the API call to get all campaigns I get this as a response:当我拨打 API 电话以获取所有活动时,我收到以下回复:

{data: {campaigns: [{id: "1", title: "Lucky's Gaming Services",…}]}}
  data: {campaigns: [{id: "1", title: "Lucky's Gaming Services",…}]}
    campaigns: [{id: "1", title: "Lucky's Gaming Services",…}]
     0: {id: "1", title: "Lucky's Gaming Services",…}
       body: "Lucky is opening a gaming service online which allows users to repair gaming 
             consoles"
       campaignType: "FC"
       couponPrice: 5
       couponsCount: 4
       creator: {id: "3", username: "test1", __typename: "UserModel"}
       id: "1"
       slug: "luckygamingservices12"
       title: "Lucky's Gaming Services"
       __typename: "CampaignModel"

Currently the campaigns array is just 1 campaign as shown above.目前,活动数组只有 1 个活动,如上所示。 Each campaign has attributes such as body, id, title, slug etc.每个广告系列都有正文、id、标题、slug 等属性。

On my react page, I basically make a call to get this response and then I handle it with this code on the main page:在我的 React 页面上,我基本上是通过调用来获取此响应,然后在主页面上使用以下代码处理它:

           {loading ? ( <h1>Loading Campaigns...</h1>
            ) : (
              data && data.campaigns.map(campaign => (
                <Grid item xs={12} sm={6} md={4} key={campaign.id}>
              <Card campaign={campaign} multiple={true}/>
              </Grid>
              ) )
            )}

Above I pass the campaign into the Card component上面我将活动传递到 Card 组件中

On the card component, how do I import the attributes of each campaign such as the body, title, id etc. so i can display that information for each campaign card in the array.在卡片组件上,我如何导入每个活动的属性,例如正文、标题、ID 等,以便我可以在数组中显示每个活动卡片的信息。

export default function Card(props) {
    const classes = useStyles();
    return (
        <>
             <Card className={classes.root}>
                        <CardActionArea>
                            <CardMedia
                            className={props.multiple ? classes.mediaMultiple : classes.media}
                            image="https://images.unsplash.com/photo-1414235077428-338989a2e8c0?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjEyMDd9"
                            title="Contemplative Reptile"
                            />
                            </CardActionArea>
                            <CardContent>
                            {props.multiple ? <Box color="#8f8f8f" mb={2} mt={0.5} fontSize={14}>Grund</Box>:
                            <Typography gutterBottom variant="h5" component="h1">
                                CAMPAIGN TITLE
                            </Typography>}
                            <Typography variant="body2" color="textSecondary" component="p">
                                CAMPAIGN BODY
                            </Typography>
                            </CardContent>
                        
                        </Card>
        </>
    )
}

Previously it worked when I did this:以前当我这样做时它起作用了:

export default function Card({campaign: {body, id, couponPrice, couponsCount, slug, title, campaignType, creator}}) {

I get this ERROR: TypeError: Cannot read property 'body' of undefined我得到这个错误: TypeError:无法读取未定义的属性'body'

How do I pass the attributes/props from the main page into the Card component?如何将属性/道具从主页传递到 Card 组件? It was working fine before but i dont know why not anymore:(它以前工作正常,但我不知道为什么不再:(

I see that you're passing campaign as a prop and then on your <Card /> component you are grabbing the props as an argument however I don't see anywhere that you are assigning body to a variable for use.我看到您将campaign作为道具传递,然后在您的<Card />组件上您将props作为参数获取,但是我没有看到任何地方将body分配给变量以供使用。

You could try iterating over the props by adding the following...您可以尝试通过添加以下内容来遍历道具...

const classes = useStyles();
const {body, id, couponPrice, couponsCount, slug, title, campaignType, creator} = props;

From there each of those should be saved to a const that you can use throughout your <Card /> component.从那里,每一个都应该保存到一个const中,您可以在整个<Card />组件中使用它。 For instance you should be able to place {body} where you want it to appear.例如,你应该能够将{body}放在你希望它出现的地方。

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

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