TypeError:无法读取React代码中未定义的属性(reading' Total')
使用Coinranking API获取硬币信息,并且代码不断丢弃此错误,我不知道有什么问题,它以前在本地运行,但是当我试图托管它时,该错误就会丢弃。现在,它也在本地抛出了错误,我认为它与全局统计初始化中的语法有关,但我不确定如何修复它。这是代码:
import React from 'react';
import millify from 'millify';
import { Typography, Row, Col, Statistic } from 'antd';
import { Link } from 'react-router-dom';
import { useGetCryptosQuery } from '../services/cryptoApi';
import Cryptocurrencies from './Cryptocurrencies';
import News from './News';
import Loader from './Loader';
const { Title } = Typography;
const Homepage = () => {
const { data, isFetching } = useGetCryptosQuery(10);
const globalStats = data?.data?.stats;
if (isFetching) return <Loader />;
return (
<>
<Title level={2} className="heading">Global Crypto Stats</Title>
<Row gutter={[32, 32]}>
<Col span={12}><Statistic title="Total Cryptocurrencies" value={globalStats.total} /></Col>
<Col span={12}><Statistic title="Total Exchanges" value={millify(globalStats.totalExchanges)} /></Col>
<Col span={12}><Statistic title="Total Market Cap:" value={`$${millify(globalStats.totalMarketCap)}`} /></Col>
<Col span={12}><Statistic title="Total 24h Volume" value={`$${millify(globalStats.total24hVolume)}`} /></Col>
<Col span={12}><Statistic title="Total Cryptocurrencies" value={globalStats.total} /></Col>
<Col span={12}><Statistic title="Total Markets" value={millify(globalStats.totalMarkets)} /></Col>
</Row>
<div className="home-heading-container">
<Title level={2} className="home-title">Top 10 Cryptos In The World</Title>
<Title level={3} className="show-more"><Link to="/cryptocurrencies">Show more</Link></Title>
</div>
<Cryptocurrencies simplified />
<div className="home-heading-container">
<Title level={2} className="home-title">Latest Crypto News</Title>
<Title level={3}><Link to="/news">Show more</Link></Title>
</div>
<News simplified />
</>
);
};
export default Homepage;
Using the coinranking API to get coin info and the code keeps on throwing this error, I have no idea what's wrong, It was running previously locally but threw that error when I attempted to host it. Now its throwing the error locally as well, I think it has something to do with the syntax in the global stats initialization but I'm not sure how to fix it. Here is the code:
import React from 'react';
import millify from 'millify';
import { Typography, Row, Col, Statistic } from 'antd';
import { Link } from 'react-router-dom';
import { useGetCryptosQuery } from '../services/cryptoApi';
import Cryptocurrencies from './Cryptocurrencies';
import News from './News';
import Loader from './Loader';
const { Title } = Typography;
const Homepage = () => {
const { data, isFetching } = useGetCryptosQuery(10);
const globalStats = data?.data?.stats;
if (isFetching) return <Loader />;
return (
<>
<Title level={2} className="heading">Global Crypto Stats</Title>
<Row gutter={[32, 32]}>
<Col span={12}><Statistic title="Total Cryptocurrencies" value={globalStats.total} /></Col>
<Col span={12}><Statistic title="Total Exchanges" value={millify(globalStats.totalExchanges)} /></Col>
<Col span={12}><Statistic title="Total Market Cap:" value={`${millify(globalStats.totalMarketCap)}`} /></Col>
<Col span={12}><Statistic title="Total 24h Volume" value={`${millify(globalStats.total24hVolume)}`} /></Col>
<Col span={12}><Statistic title="Total Cryptocurrencies" value={globalStats.total} /></Col>
<Col span={12}><Statistic title="Total Markets" value={millify(globalStats.totalMarkets)} /></Col>
</Row>
<div className="home-heading-container">
<Title level={2} className="home-title">Top 10 Cryptos In The World</Title>
<Title level={3} className="show-more"><Link to="/cryptocurrencies">Show more</Link></Title>
</div>
<Cryptocurrencies simplified />
<div className="home-heading-container">
<Title level={2} className="home-title">Latest Crypto News</Title>
<Title level={3}><Link to="/news">Show more</Link></Title>
</div>
<News simplified />
</>
);
};
export default Homepage;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
if(iSfetching&amp;&amp; globalStats)返回&lt; loader /&gt; < /code>
最初是在渲染时,globalStats可能是未定义的,这样,当它试图访问未定义的属性时,您可以防止代码破裂
Try
if (isFetching && globalStats) return <Loader />
Initially when rendering, globalStats can happen to be undefined, this way you keep code from breaking when it tries to access property of undefined
之所以发生这种情况,是因为有一个场景,
iSfetching
isfalse
和globalStats
isundefined
。当试图访问
GlobalStats
的属性时,globalStats
是未定义的
,该错误将被丢弃。有两种方法可以解决此
解决方案1:
添加一个守卫语句到
返回null
或您的加载横幅(取决于您希望您的后备是什么):解决方案2:
到处都有
globalStats。
定义您可以选择链接您的属性,例如globalStats?.myproperty
,然后您可以像以下后返回一样进行空字符串此:globalstats?.myproperty || “”
。在您的情况下,您需要将统计内容更改为以下方式:
This is happening because there is a scenario where
isFetching
isfalse
ANDglobalStats
isundefined
.When trying to access properties of
globalStats
whileglobalStats
isundefined
, that error will be thrown.There are 2 ways to solve this
Solution 1:
Add a guard statement to
return null
or your loading banner (depends what you want your fallback to be):Solution 2:
Everywhere you have
globalStats.
defined you can optional chain your properties likeglobalStats?.myProperty
and then you can do an empty string as a fallback like this:globalStats?.myProperty || ""
.In you scenario you'll need to change your stats content to this: