Walter Monecke
Walter Monecke

Reputation: 2558

Why do I need two return values within a function?

This question is very basic and very simple but I need to understand it.

I have this function in React:

renderPosts() {
  return _.map(this.props.posts, post => {
    return (
      <li className="list-group-item" key={post.id}>
        { post.title }
      </li>
    );
  });
}

Why do I need the first return?

Basically, the second return will return me the li's. Why do I need to return (first return) them again? I need someone to help me explain this part. I am just confused.

Upvotes: 2

Views: 51

Answers (2)

Layninou
Layninou

Reputation: 1

I believe it is because you are in javascript. So the first return, is the return of the function (the prototype map of your object "_" ). And the second return is the return of your map function.

Upvotes: -1

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

The first return is going to return the result of _.map(), which returns individual posts. The end result would be an array of posts as <li/> elements.

Upvotes: 2

Related Questions