0

So I am working on learning React, I am doing the Road To React book. In the book he used this code

const List = (props) => (
    props.list.map(item => (
      <div key={item.objectID}>
            <span>
              <a href={item.url}> {item.title} </a>
            </span>
            <span>
              {item.author}
            </span>
            <span>{item.num_comments} </span>
            <span>{item.points} </span>
          </div>
    ))
  )

And I used this code

const List = (props) => {
    return props.list.map(item => {
      <div key={item.objectID}>
            <span>
              <a href={item.url}> {item.title} </a>
            </span>
            <span>
              {item.author}
            </span>
            <span>{item.num_comments} </span>
            <span>{item.points} </span>
          </div>
    })
  }

His worked and mine did not work, I been researching and I do not understand why, if someone can please help me understand why.

Christian Fritz
  • 20,641
  • 3
  • 42
  • 71
SyntaxJunkie
  • 5
  • 1
  • 5

3 Answers3

1

The issue is that you changed curly bracket ( to just brace { here ...list.map(item => { and no return statement before your jsx code.

So, you can change your code to ...list.map(item => ( or just add return statement before jsx.

...list.map(item => {
    return <div...
boonya
  • 64
  • 3
0

You have to return the div in your map function.

return props.list.map(item => {
       return (<div key={item.objectID}>
            <span>
              <a href={item.url}> {item.title} </a>
            </span>
            <span>
              {item.author}
            </span>
            <span>{item.num_comments} </span>
            <span>{item.points} </span>
          </div>);
    });
Laurent Dhont
  • 1,012
  • 1
  • 9
  • 22
0

'return' is missing inside the props.list.map Since it is a function, it expects a return statement.

const List = (props) => {
    return props.list.map(item => {
      return (<div key={item.objectID}>
            <span>
              <a href={item.url}> {item.title} </a>
            </span>
            <span>
              {item.author}
            </span>
            <span>{item.num_comments} </span>
            <span>{item.points} </span>
          </div>)
    })
  }

That should solve the problem!

Gavin D'mello
  • 424
  • 4
  • 7