Why am I not able to map through array of objects. I have used this map before but it doesn't seem to be working in this component. Any idea what is wrong?
import React, { Component } from "react";
class Home extends Component {
constructor(props) {
super(props);
this.state = {
people: [
{name:"a", age: 21},
{name:"b", age: 22},
{name:"c", age: 23}
]
}
this.clickListnerHandler = this.clickListnerHandler.bind(this)
}
clickListnerHandler(e){
console.log(this.state.people)
}
render(){
return (
<div>
{this.state.people.map((detail, index) =>
{detail.name}
)}
<button
onClick={this.clickListnerHandler}
type="button" >Click on me</button>
</div>
)
}
}
export default Home
Array#mapor else you're using it wrong (and you'll end up with an array of undefineds). When you write an arrow function like() => somethingyou are implicitly returning without the keyword. When you write() => { something }you will need to explicitly write return because you've created a block. Beware of trying to return object literals though, those need parenthesesmapis not a React function, it's javascript Array's functionhttps://stackoverflow.com/questions/45189925/map-function-does-not-return-anything-in-reactjs