Usage with react-table?
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;
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.
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
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
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?
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
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: @.***>
Wow!!! That's amazing! Thank you. I will definitely use it. That sweetens the quarantine for me 😠Best regards
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?
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?