[英]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.