2

当搜索结果为空时如何显示No Resultmap()消息?

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>
        );
    }
}
4

3 回答 3

4

使用三元运算符:

<main>
   <div className="container">
     <h1>Main {offersList.length}</h1>
     { offersList.length ? offersList : <p>No result</p> }
   </div>
 </main>
于 2017-07-20T21:14:59.020 回答
3

如果offersList数组为空,它的长度将等于0。您可以制作简单的条件:

<div className="container">
  <h1>Main {offersList.length}</h1>
  { offersList.length ? offersList : <p>No Result</p> }
</div>
于 2017-07-20T21:15:17.900 回答
0
{offersList.length ? (
    // html markup with results
) : (
    // html markup if no results
)}
于 2018-12-18T13:41:59.953 回答