2

I am trying to use tippyjs-react in table cells, but tooltips don't appear.

I added tooltip into Cell option in React Table:

Cell: ({ value }) => {
    return (
      <div>
        <Tippy content="Tooltip content">
          <span>{value}</span>
        </Tippy>
      </div>
    );
  }

codesandbox

Matt
  • 8,195
  • 31
  • 115
  • 225

1 Answers1

3

The problem is that inside your Table component you change columns to render a tooltip, but you do this after your useTable call.

So a simple fix would be to put this logic before the useTable call:

function Table({ columns, data }) {
  for (let i = 0; i < columns.length; i++) {
    columns[i].columns = columns[i].columns.map((column) => ({
      ...column,
      Cell: ({ value }) => {
        return (
          <div>
            <Tippy content="Tooltip content">
              <span>{value}</span>
            </Tippy>
          </div>
        );
      },
    }));
  }

  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow,
  } = useTable({
    columns,
    data,
  });

  return (
    <table {...getTableProps()}>
      <thead>
        {headerGroups.map((headerGroup) => (
          <tr {...headerGroup.getHeaderGroupProps()}>
            {headerGroup.headers.map((column) => (
              <th {...column.getHeaderProps()}>{column.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>
  );
}

sandbox example

5eb
  • 14,798
  • 5
  • 21
  • 65