React-Table

yarn add react-table

Usage

Table.js

import React from "react";
import { useTable } from "react-table";
const debug = require("debug")("app:Table");

export default function Table({ columns, data }) {
  // Use the state and functions returned from useTable to build your UI
  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow,
  } = useTable({
    columns,
    data,
  });

  debug("getTableBodyProps", getTableBodyProps());

  // Render the UI for your table
  return (
    <table
      {...getTableProps()}
      className="w-full bg-pureWhite border border-gray-200 border-1 hover:bg-gray-50"
    >
      <thead className="p-4 font-bold border border-gray-200 border-b">
        {headerGroups.map((headerGroup) => (
          <tr {...headerGroup.getHeaderGroupProps()}>
            {headerGroup.headers.map((column) => (
              <th {...column.getHeaderProps()} className="p-4 m-0">
                {column.render("Header")}
              </th>
            ))}
          </tr>
        ))}
      </thead>
      <tbody {...getTableBodyProps()} className="p-4 cursor-pointer">
        {rows.map((row, i) => {
          prepareRow(row);
          return (
            <tr
              {...row.getRowProps()}
              className="border border-gray-200 border-b"
            >
              {row.cells.map((cell) => {
                return (
                  <td {...cell.getCellProps()} className="p-4 m-0">
                    {cell.render("Cell")}
                  </td>
                );
              })}
            </tr>
          );
        })}
      </tbody>
    </table>
  );
}

App.js

Last updated