virtual icon indicating copy to clipboard operation
virtual copied to clipboard

Usage with react-table?

Open deadcoder0904 opened this issue 6 years ago • 29 comments

I cloned the react-table's Simple sandbox to get react-virtual working with it.

But I'm confused what to put instead of rowVirtualizer.virtualItems when using .map() inside tbody?

Also, confused if I have to wrap the whole table or just tbody as it's the only thing that needs scrolling.

I think all the rows get rendered because the rendering is too slow for only 2000 items. I'm sure I must be doing something wrong.

Here's my CodeSandbox 👉 https://codesandbox.io/s/react-table-basic-with-react-virtual-tq047?file=/src/App.js

And here's the same code if I ever delete it:

import React from "react";
import styled from "styled-components";
import { useTable } from "react-table";
import { useVirtual } from "react-virtual";

import makeData from "./makeData";

const Styles = styled.div`
  padding: 1rem;

  table {
    border-spacing: 0;
    border: 1px solid black;

    tr {
      :last-child {
        td {
          border-bottom: 0;
        }
      }
    }

    th,
    td {
      margin: 0;
      padding: 0.5rem;
      border-bottom: 1px solid black;
      border-right: 1px solid black;

      :last-child {
        border-right: 0;
      }
    }
  }
`;

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
  });

  const parentRef = React.useRef();
  const rowVirtualizer = useVirtual({
    size: rows.length,
    parentRef,
    estimateSize: React.useCallback(i => rows[i], []),
    overscan: 5
  });

  // Render the UI for your table
  return (
    <div
      ref={parentRef}
      className="List"
      style={{
        height: `450px`,
        width: `650px`,
        overflow: "auto"
      }}
    >
      <div
        className="ListInner"
        style={{
          height: `${rowVirtualizer.totalSize}px`,
          width: "100%",
          position: "relative"
        }}
      >
        <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>
      </div>
    </div>
  );
}

function App() {
  const columns = React.useMemo(
    () => [
      {
        Header: "Name",
        columns: [
          {
            Header: "First Name",
            accessor: "firstName"
          },
          {
            Header: "Last Name",
            accessor: "lastName"
          }
        ]
      },
      {
        Header: "Info",
        columns: [
          {
            Header: "Age",
            accessor: "age"
          },
          {
            Header: "Visits",
            accessor: "visits"
          },
          {
            Header: "Status",
            accessor: "status"
          },
          {
            Header: "Profile Progress",
            accessor: "progress"
          }
        ]
      }
    ],
    []
  );

  const data = React.useMemo(() => makeData(2000), []);

  return (
    <Styles>
      <Table columns={columns} data={data} />
    </Styles>
  );
}

export default App;

deadcoder0904 avatar May 09 '20 12:05 deadcoder0904

Hard to say, first you need to identify what is slow? Nothing new, but checkout points here https://github.com/TanStack/react-virtual/issues/74#issuecomment-822301154

Also pin v2.8.2 version, in 2.9 we introduce some performance regression to re-render on every scroll.

piecyk avatar Feb 25 '22 17:02 piecyk

Made an example by combining @piecyk two approaches https://codesandbox.io/s/poc-react-virtual-table-forked-857kdi?file=/src/index.js

In a nutshell used dumb trs for adding space

a9a4k avatar Mar 31 '22 19:03 a9a4k

Made an example by combining @piecyk two approaches https://codesandbox.io/s/poc-react-virtual-table-forked-857kdi?file=/src/index.js

In a nutshell used dumb trs for adding space

Wow, this works surprisingly well for how simple it is

KevinVandy avatar Jun 06 '22 23:06 KevinVandy

Made an example by combining @piecyk two approaches https://codesandbox.io/s/poc-react-virtual-table-forked-857kdi?file=/src/index.js

In a nutshell used dumb trs for adding space

I tried a similar approach with dumb <td>s to get column virtualization to work, but it didn't really work. Anyone else try anything similar?

KevinVandy avatar Jun 09 '22 17:06 KevinVandy

Hey @tannerlinsley, thank you for your great open source work! Please checkout the following virtualization demo of tbody https://html-next.github.io/vertical-collection/#/examples/dbmon

vertical-collection is an Ember.js project and they managed to virtualize a table with table-markup. Since I switched to react, I have been searching for a similar possibility in react. For now without success. Would something be possible with this project. I am very interested in finding a solution. Sometimes it's not possible to switch to divs. If you think it could be possible (maybe some insights about the difficulties), then I will invest time to make it working.

I already posted there a question, maybe you are interested https://github.com/html-next/vertical-collection/issues/355

Thank you Best regards

wuarmin avatar Jul 15 '22 09:07 wuarmin

We have one now! https://tanstack.com/table/v8/docs/examples/react/virtualized-rows

Tanner Linsley On Jul 15, 2022, 3:35 AM -0600, Armin @.***>, wrote:

Hey @tannerlinsley, thank you for your great open source work! Please checkout the following virtualization demo of tbody https://html-next.github.io/vertical-collection/#/examples/dbmon vertical-collection is an Ember.js project and they managed to virtualize a table with table-markup. Since I switched to react, I have been searching for a similar possibility in react. For now without success. Would something be possible with this project. I am very interested in finding a solution. Sometimes it's not possible to switch to divs. Best regards — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

tannerlinsley avatar Jul 15 '22 14:07 tannerlinsley

Wow!!! That's amazing! Thank you. I will definitely use it. That sweetens the quarantine for me 😭 Best regards

wuarmin avatar Jul 15 '22 14:07 wuarmin

We have one now! https://tanstack.com/table/v8/docs/examples/react/virtualized-rows Tanner Linsley … On Jul 15, 2022, 3:35 AM -0600, Armin @.>, wrote: Hey @tannerlinsley, thank you for your great open source work! Please checkout the following virtualization demo of tbody https://html-next.github.io/vertical-collection/#/examples/dbmon vertical-collection is an Ember.js project and they managed to virtualize a table with table-markup. Since I switched to react, I have been searching for a similar possibility in react. For now without success. Would something be possible with this project. I am very interested in finding a solution. Sometimes it's not possible to switch to divs. Best regards — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.>

Hi, this example is using react-virtual library. How about @tanstack/react-virtual?

bitweedx86 avatar Nov 29 '22 10:11 bitweedx86

We have one now! https://tanstack.com/table/v8/docs/examples/react/virtualized-rows Tanner Linsley … On Jul 15, 2022, 3:35 AM -0600, Armin @.>, wrote: Hey @tannerlinsley, thank you for your great open source work! Please checkout the following virtualization demo of tbody https://html-next.github.io/vertical-collection/#/examples/dbmon vertical-collection is an Ember.js project and they managed to virtualize a table with table-markup. Since I switched to react, I have been searching for a similar possibility in react. For now without success. Would something be possible with this project. I am very interested in finding a solution. Sometimes it's not possible to switch to divs. Best regards — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.>

@tannerlinsley If I'm using @tanstack/react-virtual in this example, the virtualization is not being handled properly? Why so?

manisha-planetrics avatar May 09 '23 05:05 manisha-planetrics