繁体   English   中英

Reactjs 如果没有从 api 获取数据,我将如何显示一个简单的错误?

[英]Reactjs How would I display a simple error if no data is fetched from api?

我正在努力寻找一种添加错误消息的方法是来自 api === 0 的过滤国家。我如何最好地做到这一点,这是一种简单的方式? 或者是否有一种简单的方法可以路由到 404 页面?

我可以按照正确的思路思考它只是没有点击!

const CountryList = ({ filteredCountries, isLoading }) => {
  return (
    <div className='card-list'>
      {isLoading ? (
        <Loader isLoading={isLoading} />
      ) : (
        filteredCountries.map((country) => (
          <>
            <Country
              key={country.name}
              id={country.alpha3Code}
              country={country}
            />
          </>
        ))
      )}
    </div>
  );
};
export default CountryList;

您可以形成另一个条件并通过过滤国家的length检查。 从技术上讲,它本身不会被视为错误,但据我所知,这就是您想要的:

const CountryList = ({ filteredCountries, isLoading }) => {
  return (
    <div className='card-list'>
      {isLoading ? (
        <Loader isLoading={isLoading} />
      ) : (
        filteredCountries.length ? (
          'No countries found'
        ) : (
          filteredCountries.map((country) => (
            <>
              <Country
                key={country.name}
                id={country.alpha3Code}
                country={country}
              />
            </>
        )))
      )}
    </div>
  );
};
export default CountryList;

暂无
暂无

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

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