简体   繁体   English

在卡片组件中显示道具的问题 - React

[英]Issue with displaying props in Card Component - React

I am building a tracker application in React using an open source API.我正在使用开源 API 在 React 中构建跟踪器应用程序。 I am using Axios for HTTP requests.我正在使用 Axios 来处理 HTTP 请求。 When I pass the objects retrieved from the GET function in index.js, I get an error that reads:当我在 index.js 中传递从 GET function 检索到的对象时,我收到一条错误消息:

TypeError: Cannot read property 'NewConfirmed' of undefined TypeError:无法读取未定义的属性“NewConfirmed”

Does anyone know why this may be happening based on the code below and why the parameters are coming back undefined?有谁知道为什么会根据下面的代码发生这种情况以及为什么参数返回未定义? I tried to change my de-structuring of the parameters to reflect the way the API structures it, but it did not work.我试图改变我对参数的解构以反映 API 的结构方式,但它不起作用。

index.js index.js

import axios from 'axios';

const url = 'https://api.covid19api.com/summary';

export const fetchData = async () => {
    try {
        const { data: { Global: { NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths, NewRecovered, TotalRecovered, } } } = await axios.get(url);


        return { NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths, NewRecovered, TotalRecovered,  } ;
    } catch (error) {
        console.log(error);

    }
}

App.js应用程序.js

import React from 'react';

import {Cards, Chart, CountryPicker} from './components';
import styles from './App.module.css';
import {fetchData} from './api';

class App extends React.Component {

    state ={
        data: {},
    }

    async componentDidMount() {
        const fetchedData = await fetchData();

        this.setState({data: fetchedData});

    }

    render() {

        const { data } = this.state;
        return(
            <div className={styles.container}>
                <Cards data = {data} />
                <CountryPicker />
                <Chart />
            </div>
        );
    }
}

export default App;

Cards.jsx卡片.jsx

import React from 'react';
import {Card, CardContent, Typography, Grid} from '@material-ui/core';
import styles from './Cards.module.css';

const Cards = ({data: { Global: { NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths, NewRecovered, TotalRecovered } } }) => {    


    return(
        <div className = {styles.container}>
            <Grid container spacing={3} justify="center">
                <Grid item component={Card}>
                    <CardContent>
                        <Typography color="textSecondary" gutterBottom>New Confirmed</Typography>
                        <Typography variant="h5">{NewConfirmed.value}</Typography>
                        <Typography color="textSecondary">REAL DATE</Typography>
                        <Typography variant="body2">Number of active cases of COVID-19.</Typography>
                    </CardContent>
                </Grid>
                <Grid item component={Card}>
                    <CardContent>
                        <Typography color="textSecondary" gutterBottom>Total Confirmed</Typography>
                        <Typography variant="h5">{TotalConfirmed.value}</Typography>
                        <Typography color="textSecondary">REAL DATE</Typography>
                        <Typography variant="body2">Number of active cases of COVID-19.</Typography>
                    </CardContent>
                </Grid>
                <Grid item component={Card}>
                    <CardContent>
                        <Typography color="textSecondary" gutterBottom>New Deaths</Typography>
                        <Typography variant="h5">{NewDeaths.value}</Typography>
                        <Typography color="textSecondary">REAL DATE</Typography>
                        <Typography variant="body2">Number of active cases of COVID-19.</Typography>
                    </CardContent>
                </Grid>
                <Grid item component={Card}>
                    <CardContent>
                        <Typography color="textSecondary" gutterBottom>Total Deaths</Typography>
                        <Typography variant="h5">REAL DATA</Typography>
                        <Typography color="textSecondary">REAL DATE</Typography>
                        <Typography variant="body2">Number of recoveries of COVID-19.</Typography>
                    </CardContent>
                </Grid>
                <Grid item component={Card}>
                    <CardContent>
                        <Typography color="textSecondary" gutterBottom>New Recovered</Typography>
                        <Typography variant="h5">REAL DATA</Typography>
                        <Typography color="textSecondary">REAL DATE</Typography>
                        <Typography variant="body2">Number of deaths of COVID-19.</Typography>
                    </CardContent>
                </Grid>
                <Grid item component={Card}>
                    <CardContent>
                        <Typography color="textSecondary" gutterBottom>Total Recovered</Typography>
                        <Typography variant="h5">REAL DATA</Typography>
                        <Typography color="textSecondary">REAL DATE</Typography>
                        <Typography variant="body2">Number of deaths of COVID-19.</Typography>
                    </CardContent>
                </Grid>
            </Grid>
        </div>
    )

}

export default Cards;

You are fetching API results after the compountDidMount (ie when the render method is called).您在 compountDidMount 之后(即调用渲染方法时)获取 API 结果。 So your component is rendering without any data and NewConfirmed is undefined since your API call didn't finish yet.因此,您的组件在没有任何数据的情况下呈现,并且NewConfirmed未定义,因为您的 API 调用尚未完成。

Try adding a check before rendering the component.在渲染组件之前尝试添加检查。

 import React from 'react'; import {Card, CardContent, Typography, Grid} from '@material-ui/core'; import styles from './Cards.module.css'; const Cards = ({data: { Global: { NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths, NewRecovered, TotalRecovered } } }) => { if (;NewConfirmed) return null. return( <div className = {styles.container}> <Grid container spacing={3} justify="center"> <Grid item component={Card}> <CardContent> <Typography color="textSecondary" gutterBottom>New Confirmed</Typography> <Typography variant="h5">{NewConfirmed.value}</Typography> <Typography color="textSecondary">REAL DATE</Typography> <Typography variant="body2">Number of active cases of COVID-19.</Typography> </CardContent> </Grid> <Grid item component={Card}> <CardContent> <Typography color="textSecondary" gutterBottom>Total Confirmed</Typography> <Typography variant="h5">{TotalConfirmed.value}</Typography> <Typography color="textSecondary">REAL DATE</Typography> <Typography variant="body2">Number of active cases of COVID-19.</Typography> </CardContent> </Grid> <Grid item component={Card}> <CardContent> <Typography color="textSecondary" gutterBottom>New Deaths</Typography> <Typography variant="h5">{NewDeaths.value}</Typography> <Typography color="textSecondary">REAL DATE</Typography> <Typography variant="body2">Number of active cases of COVID-19.</Typography> </CardContent> </Grid> <Grid item component={Card}> <CardContent> <Typography color="textSecondary" gutterBottom>Total Deaths</Typography> <Typography variant="h5">REAL DATA</Typography> <Typography color="textSecondary">REAL DATE</Typography> <Typography variant="body2">Number of recoveries of COVID-19.</Typography> </CardContent> </Grid> <Grid item component={Card}> <CardContent> <Typography color="textSecondary" gutterBottom>New Recovered</Typography> <Typography variant="h5">REAL DATA</Typography> <Typography color="textSecondary">REAL DATE</Typography> <Typography variant="body2">Number of deaths of COVID-19.</Typography> </CardContent> </Grid> <Grid item component={Card}> <CardContent> <Typography color="textSecondary" gutterBottom>Total Recovered</Typography> <Typography variant="h5">REAL DATA</Typography> <Typography color="textSecondary">REAL DATE</Typography> <Typography variant="body2">Number of deaths of COVID-19;</Typography> </CardContent> </Grid> </Grid> </div> ) } export default Cards;

in fetchData your reading a Global object but you're returning just the content of the Global object not Global itself.在 fetchData 您阅读 Global object 但您只返回 Global object 的内容而不是 Global 本身。 Then in Cards you're receiving props as Global again, but it's just the Global content.然后在 Cards 中,您再次收到作为 Global 的道具,但这只是 Global 内容。

You have:你有:

const Cards = ({data: { Global: { NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths...

But it should be:但它应该是:

const Cards = ({data: { NewConfirmed, TotalConfirmed, NewDeaths, TotalDeaths...

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

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