Insert ref in RowRenderer not possible
Hello and happy new year!
I added a ref in rowRenderer, this is needed in my DataGrid for infinite scroll. I'm using the IntersectionObserver for this solution. That means, if last row intersects with DOM, it will get fired in "isIntersecting" to true and then fetching new data from the BackEnd. This works well, when working with an array and adding the ref to last entry by a ternery.
But when I use the rowRenderer, ref will not be added to Row.:
return (
<Row
{...props}
onMouseDown={() => handleOnClick(props.row.id)}
ref={rows.length - 1 === props.rowIdx ? lastRowRef : null}
/>
);
Also when I surround it with a div like following, it is not working:
return (
<div ref={rows.length - 1 === props.rowIdx ? lastRowRef : null}>
<Row {...props} />
</div>
);
What can I do here? Did I missed something?
Thanks a lot!
I'm also not able to use ref inside DraggableRowRenderer. Sandbox link here.
AFAICT the ref works, but you might want to use a state instead of a ref, so that your parent component can be updated once the "ref" is set.
Something like that should work:
function MyGrid() {
const [lastRow, setLastRow] = useState(null);
useEffect(() => {
// intersection check here
}, [lastRow]);
return (
<DataGrid
{...props}
rowRenderer={props => <Row {...props} ref={props.rowIdx === rows.length - 1 ? setLastRow : null} />}
/>
);
}
Thank you for your suggestion. This works in so far as lastRow is no longer null, more precisely lastRow is then the DOM element, which is generally not so bad.
However, this triggers already when rendering the grid and for an Infinite scroll I need this only when the last row becomes "visible". So I can't react to it in the useEffect and also not in the onScroll event.
Assuming I have 100 rows and see 20, then your suggestion triggers when I see the first 20, but should only trigger when the hundredth row becomes visible.
I have been able to successfully implement this solution for myself, using setRows() as below. But in this case I really need the rowRenderer. A combination of setRows and rowRenderer doesn't work either, because the "ref" is "buried way down" in the props object, so the DOM doesn't react to it.
`const createRows = () => { let i = 0;
content.map((user) => {
rows.push({
id: user.id,
name: user.name,
email:
i < content.length - 1 ? (
user.email
) : (
<div ref={lastRowRef}>{user.email}</div>
),
});
i += 1;
});
return rows;
};`
the "ref" is "buried way down" in the props object, so the DOM doesn't react to it.
What do you mean?
Could this work instead? We don't really need a ref or a extra state.
function MyGrid() {
function createRows() {
// ...
}
return (
<DataGrid
{...props}
rowRenderer={props => {
if (props.rowIdx === rows.length - 1) {
// last row is rendered, create rows here
createRows();
}
return <Row {...props} />;
}}
/>
);
}
Closing due lack of activity. Please create a a new issue if you are still facing problems.