2

I have a mobx store from where I am pulling an array of data and trying to render each as an <li>

 let completedList = completedTodos.map(todo => { <li> {todo.value} </li> } );

I can't figure out why this does not return anything but undefined.

Sorry I am new here and this is probably a dumb question.

4
  • 1
    you are not returning anything inside map body use this: let completedList = completedTodos.map(todo => <li> {todo.value} </li> ); or use return to return the html element, like this: let completedList = completedTodos.map(todo => {return <li> {todo.value} </li> }); Commented May 16, 2017 at 19:45
  • 2
    @blackmind check my first comment again, i mentioned both ways :) Commented May 16, 2017 at 19:50
  • 1
    This worked perfect. Thanks for showing me the way. Commented May 16, 2017 at 19:57
  • For reference this is how I got my code to work. render(){ let completedList = completedTodos.map(todo => <li> {todo.value} </li>); return ( <div> <h1>todos</h1> {completedList} </div> ) } Commented May 16, 2017 at 20:26

1 Answer 1

4

Easy mistake! Your arrow function should use parentheses () instead of brackets {} if you don't explicitly use return.

let completedList = completedTodos.map(todo => (<p>{todo.value}</p>));

Otherwise, you can skip the parens entirely - the arrow function without brackets implies a return.

let completedList = completedTodos.map(todo => <p>{todo.value}</p>);
Sign up to request clarification or add additional context in comments.

2 Comments

You can also just do completedTodos.map(todo => <p>{todo.value}</p>); but good catch on the {}
Yep, @mayank-shukla and @blackmind are right - you don't even need the parens let completedList = completedTodos.map(todo => <p>{todo.value}</p> );

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.