reactjs - 在我的 React 项目中获取“无法将类作为函数调用”:throw new TypeError("Cannot call a class as a function");
问题描述
我正在制作 COVID-19 跟踪器项目,但我遇到了这个错误,我被卡住了,不确定它来自哪里。
import { Card, CardContent, Typography, Grid } from '@material-ui/core';
import CountUp from 'react-countup';
import cx from 'react-countup';
import styles from './Cards.module.css';
const CardComponent = ({ className, cardTitle, value, lastUpdate, cardSubtitle }) => (
<Grid item xs={12} md={3} component={Card} className={cx(styles.card, className)}>
<CardContent>
<Typography color="textSecondary" gutterBottom>
{cardTitle}
</Typography>
<Typography variant="h5" component="h2">
<CountUp start={0} end={value} duration={2.75} separator="," />
</Typography>
<Typography color="textSecondary">
{new Date(lastUpdate).toDateString()}
</Typography>
<Typography variant="body2" component="p">
{cardSubtitle}
</Typography>
</CardContent>
</Grid>
);
const Info = ({ data: { confirmed, recovered, deaths, lastUpdate } }) => {
if (!confirmed) {
return 'Loading...';
}
return (
<div className={styles.container}>
<Typography gutterBottom variant="h4" component="h2">Global</Typography>
<Grid container spacing={3} justify="center">
<CardComponent
className={styles.infected}
cardTitle="Infected"
value={confirmed.value}
lastUpdate={lastUpdate}
cardSubtitle="Number of active cases from COVID-19."
/>
<CardComponent
className={styles.recovered}
cardTitle="Recovered"
value={recovered.value}
lastUpdate={lastUpdate}
cardSubtitle="Number of recoveries from COVID-19."
/>
<CardComponent
className={styles.deaths}
cardTitle="Deaths"
value={deaths.value}
lastUpdate={lastUpdate}
cardSubtitle="Number of deaths caused by COVID-19."
/>
</Grid>
</div>
);
};
export default Info;```
解决方案
您不会在组件中返回 JSX 元素,而是返回一个简单的字符串。
if (!confirmed) {
return 'Loading...';
}
推荐阅读
- java - 这两个 Java 代码哪个更快?
- android-studio - 如何更改 FlutterLogo 图像
- javascript - 不能使用简单地图?
- angular - 包含公共对话框的组件:MatDialog,在构造函数中无法加载
- php - 调用未定义的函数 mysql_select_db()
- cakephp-2.3 - 如何生成类别树,显示技术详细信息表中的相应评级
- javascript - 如何使用 jquery 在 laravel 中单击下拉菜单中获取 li 的值?
- php - 使代码更小的可能性
- angularjs - 将 angularJS 值发送到 MVC 操作
- angular - 角材料2不起作用