javascript - 如何在 react.js 的地图中使用 if?
问题描述
我有这个函数,我有这段代码,但它不返回任何东西,只是留空
mostrarPostsNoValidos() {
if(this.state.posts.length > 0) {
this.state.posts.map((post, key) => {
if(post.validado === "0"){
return (
<a href={`/post/${post.id}/${post.slug}`} key={key}>
<div className="card post">
<div className="card-body">
{post.titulo}
</div>
</div>
</a>
);
}
});
} else {
return <h3>No hay posts que mostrar aún</h3>
}
}
解决方案
解决方案:添加“return this.state.posts.map”
mostrarPostsNoValidos() {
if(this.state.posts.length > 0) {
return this.state.posts.map((post, key) => {
if(post.validado === "0"){
return (
<a href={`/post/${post.id}/${post.slug}`} key={key}>
<div className="card post">
<div className="card-body">
{post.titulo}
</div>
</div>
</a>
);
}
});
} else {
return <h3>No hay posts que mostrar aún</h3>
}
}