Skip to content

Instantly share code, notes, and snippets.

@Blezzoh
Last active February 24, 2020 23:56
Show Gist options
  • Save Blezzoh/0fb6227e0916cf693511dfda6972231b to your computer and use it in GitHub Desktop.
Save Blezzoh/0fb6227e0916cf693511dfda6972231b to your computer and use it in GitHub Desktop.
import React from "react";
import { Table } from "react-bootstrap";
import { useTable } from "react-table";
/**
* As in the previous versions, a react-table accepts colums where at the core we have a field Header, and accessor
* As in the previous versions, a react-table has data that consist of an array of JSONs
*/
const ReactTable = ({ columns, data }) => {
// you can get the react table functions by using the hook useTable
const {
getTableProps,
getTableBodyProps,
headerGroups,
rows,
prepareRow
} = useTable({
columns,
data
});
return (
<Table bordered {...getTableProps()}>
<thead>
{headerGroups.map(headerGroup => (
<tr {...headerGroup.getHeaderGroupProps()}>
{headerGroup.headers.map(column => {
const {render, getHeaderProps} = column
return (
<th {...getHeaderProps()}>{render("Header")}</th>
)
})}
</tr>
))}
</thead>
<tbody {...getTableBodyProps()}>
{rows.map((row, i) => {
prepareRow(row);
return (
<tr {...row.getRowProps()}>
{row.cells.map(cell => {
return <td {...cell.getCellProps()}>{cell.render("Cell")}</td>;
})}
</tr>
);
})}
</tbody>
</Table>
);
};
export default ReactTable;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment