So I’m trying to render data from an object, and I need to pass the object as a prop to the component. My strategy has been to use the .map() method to map over the Object.keys() array. However, when I attempt to pass in {props.data} to the method I’m using to do this, I get an "Unexpected token, expected , " error. But when I put in props.data without JSX brackets (as I do in the code below), no error gets thrown. However, the entire component fails to render. So I’m kind of stuck between a rock and a hard place. Is there something obvious I’m missing here? Thanks!
const data = {
object1: {
name: 'name1',
},
object2: {
name: 'name2',
}
}
class Center extends React.Component {
constructor (props){
super (props)
this.renderObj = this.renderObj.bind(this);
}
// methods
renderObj = () => {
Object.keys(props.data).map((obj, i) => {
return (
<div>
{props.data[obj].name}
</div>
)})}
render () {
return(
<div>
{this.renderObj()}
<h1>4</h1>
</div>
)
}
}
class App extends React.Component {
constructor (props){
super (props)
}
render () {
return(
<Center data={data} />
)
}
}
ReactDOM.render(<App/>, document.getElementById("root"));