jui*_*y j 1 conditional-statements reactjs conditional-rendering
我正在尝试找出 React 中的条件渲染。如果用户的观看列表中没有电影,我只想输出一个标题。我认为这样的事情会起作用:
render() {
return (
<Container>
{this.state.watchlist.map(item => {
if(this.state.watchlist.length > 0) {
return (
<WatchlistMovie
className="watchlist-movie"
key={item.id}
id={item.id}
title={item.title}
poster={item.poster}
overview={item.overview}
rating={item.rating}
user={this.props.user}
/>
);
} else {
return <h1>no movies</h1>
}
)}}
</Container>
);
}
Run Code Online (Sandbox Code Playgroud)
我相信你想要if-else外部的逻辑map
<Container>
{this.state.watchlist.length === 0 && <h1>no movies</h1>}
{this.state.watchlist.map(item => (<WatchlistMovie
className="watchlist-movie"
key={item.id}
id={item.id}
title={item.title}
poster={item.poster}
overview={item.overview}
rating={item.rating}
user={this.props.user}
/>))}
</Container>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4460 次 |
| 最近记录: |