-1

I have this function where I have this piece of code, but it does not return anything, just leave blank

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>
    }
}
3
  • 3
    Perfect! Leave it as is. Commented Jun 12, 2018 at 19:58
  • 1
    return this.state.posts.map... Commented Jun 12, 2018 at 20:01
  • 1
    Your return statement is returning from map's callback function. Your outer function mostrarPostsNoValidos doesn't return anything if if condition is equal to true. Just add return on the third line before map invocation. Although, I should mention that map is not the perfect suit for your case. More like reduce or you'll have to wrap map within filter. Commented Jun 12, 2018 at 20:09

1 Answer 1

0

Solution: add "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>
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not even citing where you got the solution from - to your own question..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.