I'm attempting to render a table using ReactJS, but get an error -Cannot read property 'map' of undefined -
The code:
import React, { Component } from 'react';
import './Table.css'
const obj = [{
name: "onion",
price: ".99",
id: 1
}, {
name: "pepper",
price: "1.25",
id: 2
}, {
name: "broccoli",
price: "3.00",
id: 3
}];
class TableRow extends React.Component {
render() {
const {
data
} = this.props;
const row = data.map((data) =>
<tr>
<td key={data.name}>{data.name}</td>
<td key={data.id}>{data.id}</td>
<td key={data.price}>{data.price}</td>
</tr>
);
return (
<span>{row}</span>
);
}
}
class Table extends Component {
constructor(props) {
super(props)
}
render() {
return (
<table>
<TableRow data={this.props.data} />
</table>
);
}
}
export default Table;
I imported the table component into the App.js file. However, the error occurs on the following line of Table.js: const row = data.map((data) => ...could I get some assistance please?
dataisundefined. Are you sure you are getting the data at the time of the render?Tableitsdataprop? Is thatdataan array? As an aside,data.map((data) => ---)is wrong, you can't define 2 variables with the same name in the same scopeconst obj = ...the same thing asdata?