gpt4 book ai didi

javascript - react : How to show message when result is zero in react

转载 作者:行者123 更新时间:2023-12-03 22:32:24 26 4
gpt4 key购买 nike

如何在 map() 中搜索结果为空时显示No Result 消息?

export class Properties extends React.Component {
render () {
const { data, searchText } = this.props;
const offersList = data
.filter(offerDetail => {
return offerDetail.city.toLowerCase().indexOf(searchText.toLowerCase()) >= 0;
})
.map(offerDetail => {
return (
<div className="offer" key={offerDetail.id}>
<h2 className="offer-title">{offerDetail.title}</h2>
<p className="offer-location"><i className="location-icon"></i> {offerDetail.city}</p>
</div>
);
});
return (
<main>
<div className="container">
<h1>Main {offersList.length}</h1>
{ offersList }
</div>
</main>
);
}
}

最佳答案

使用三元运算符:

<main>
<div className="container">
<h1>Main {offersList.length}</h1>
{ offersList.length ? offersList : <p>No result</p> }
</div>
</main>

关于javascript - react : How to show message when result is zero in react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45225266/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com