I am trying to display a table which fetches data from a server and displays all the information in it. The code is printing my table header and information of the first object from the fetched API.
Its giving me an error.
Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of
MyTable
"
import React from "react";
export default class MyTable extends React.Component {
constructor(props) {
super(props);
console.log(props);
}
createTable = () => {
let table = [];
let tableHeader = (
<thead>
<tr>
{this.props.columns.map(column => {
return <th key={column.name}>{column.name}</th>;
})}
</tr>
</thead>
);
let tableRows = [];
for (
let i = this.props.pgNo * this.props.maxItems;
i < i + this.props.maxItems;
i++
) {
if (i > this.props.users.length) {
break;
} else {
let row = (
<tr>
{this.props.columns.map(column => {
return (
<td key={column.key}>{this.props.users[i][column.key]}</td>
);
})}
</tr>
);
tableRows.push(row);
}
let tableBody = <tbody>{tableRows}</tbody>;
return (
<table>
{table}
{tableHeader}
{tableBody}
</table>
);
}
};
render() {
return <div className="col-sm-10">{this.createTable()}</div>;
}
}