Repository navigation
Closed
Description
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;Metadata
Metadata
Assignees
Labels
No labels
Activity
tannerlinsley commentedon May 9, 2020
<table>elements. You'll need to use divs and one of the flex/absolute/block layout plugins from react-tabledeadcoder0904 commentedon May 9, 2020
What do you mean by elements here?
But isn't this a virtualization library? So what do I need to use for virtualizing tables with tons of data (10k+ rows)? Should I use
react-windowinstead?tannerlinsley commentedon May 9, 2020
Sorry I’ve updated my original comment. For some reason I forgot the back ticks around
<table>so it just skipped over the code snippet loltannerlinsley commentedon May 9, 2020
Also, you misunderstood my second point. You can easily virtualize a grid with this library. There are plenty of examples showing that. However, Getting the correct markup to virtualize a complex grid with persistently visible scroll bars and fixed headers is independent of what library you use. If you used React Window, you would still have the same challenges in that scenario.
deadcoder0904 commentedon May 9, 2020
Thanks @tannerlinsley
I understood I need to put the virtualized code in
tbodysomewhere. Mind answering 1 of my earlier questions 👇Also, a simple example in the
examples/folder would be more than helpful I think. I mean one withreact-table:)tannerlinsley commentedon May 9, 2020
I will work on an example. Again I need to reiterate, you can’t virtualize a tbody element and you can wrap it in anything that will allow you to. You have to use divs.
deadcoder0904 commentedon May 9, 2020
Yes I meant putting the
divsinsidetbodylike:tannerlinsley commentedon May 9, 2020
Yes conceptually that is how you do it. But that’s invalid html. You can’t have divs as the child of tbody and unless you style the divs as display table or the tbody as a div, that won’t really work either way. All divs. That is the way.
eirikaho commentedon May 25, 2020
ETA on this? 👼
piecyk commentedon Feb 16, 2021
@tannerlinsley what if we would use pseudo elements on tbody and control them via css custom properties 🤔
https://codesandbox.io/s/poc-react-virtual-table-jyz0m
jonahallibone commentedon Oct 28, 2021
@piecyk your code works great for me -- except I have no idea what it's doing. Can you explain?
tannerlinsley commentedon Jan 20, 2022
This is genius. I saw something similar with other table libraries. Let's make sure this pattern is well-known and supported from here on out.
2 remaining items
tannerlinsley commentedon Feb 25, 2022
chrisfernandes102 commentedon Feb 25, 2022
@tannerlinsley I am preparing rows that are rendering but I guess my issue is that there seem to be some performance issues with my rows showing in the table using react virtual. The code very much follows what @piecyk did in his awesome example. It almost seems as if it is not rendering the rows fast enough in the dom.
chrisfernandes102 commentedon Feb 25, 2022
@tannerlinsley for reference, this is what my table component looks like:
piecyk commentedon Feb 25, 2022
@chrisfernandes102 please create codesandbox with the issue, then we can discuss more.
piecyk commentedon Feb 25, 2022
Another approach would be include extra rows rather pseudo elements on tbody, also fyi when having a sticky header remember to subtract its height.
https://codesandbox.io/s/poc-react-virtual-table-forked-p08p9h?file=/src/index.js
chrisfernandes102 commentedon Feb 25, 2022
@piecyk @tannerlinsley
The code I have is probably a bit too complex (and large) to throw into a code sandbox. The overall question I have is how do I allow the scroll to smoothly show data in the DOM, right now it grabs the virtual rows and the experience looks so laggy.
piecyk commentedon Feb 25, 2022
Hard to say, first you need to identify what is slow? Nothing new, but checkout points here #74 (comment)
Also pin v2.8.2 version, in 2.9 we introduce some performance regression to re-render on every scroll.
a9a4k commentedon Mar 31, 2022
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 spaceKevinVandy commentedon Jun 6, 2022
Wow, this works surprisingly well for how simple it is
KevinVandy commentedon Jun 9, 2022
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?wuarmin commentedon Jul 15, 2022
Hey @tannerlinsley,
thank you for your great open source work!
Please checkout the following virtualization demo of
tbodyhttps://html-next.github.io/vertical-collection/#/examples/dbmonvertical-collectionis 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 todivs. 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
html-next/vertical-collection#355
Thank you
Best regards
tannerlinsley commentedon Jul 15, 2022
wuarmin commentedon Jul 15, 2022
Wow!!! That's amazing! Thank you. I will definitely use it. That sweetens the quarantine for me 😭
Best regards
bitweedx86 commentedon Nov 29, 2022
Hi, this example is using
react-virtuallibrary. How about@tanstack/react-virtual?manisha-planetrics commentedon May 9, 2023
@tannerlinsley If I'm using
@tanstack/react-virtualin this example, the virtualization is not being handled properly? Why so?