Sandy Sandy - 1 month ago 5
React JSX Question

Using ES6 prototype method map rendered nothing in React

Anything wrong with my map method?

var App = React.createClass({
getInitialState(){
return {
items:[1,2,3]
}
},
renderItem(){
return(
this.state.items.map((item,i))=>
<li key={i}> {item} </li>
)
},
render(){
return(
<ul>
{this.renderItem()}
</ul>
)
}
})


Couldn't see anything rendered, checked the console, no error found.

Answer

You have a syntax problem:

this.state.items.map((item,i))=>
                             ^

Remove that close parenthesis, and place it beside the next close parenthesis:

return(
  this.state.items.map((item,i)=>
  <li key={i}> {item} </li> 
))
Comments