I am having an issue replacing this useEffect+useRef with a useCallback: all code and line of issue.
Also here's a short snippet for my question if you don't want to check all code above:
const gridAreaRef = useCallback((node) => {
console.log(node);
console.log(node.querySelector(".ag-center-cols-container"));
//...
}
return (
<div className="ag-theme-alpine" style={{ height: 400 }} ref={gridAreaRef}>
When I console.log(node)
I see it on the console. Also when I expand the console logged node
as an element in the console, I see all its DOM tree and elements nested in it including one with the class .ag-center-cols-container
. However, when I console.log(node.querySelector(".ag-center-cols-container"))
I get null!
Actually, the currently used useEffect works in the example above, but doesn't work in the actual project I'm using AgGridReact. It is expected if it doesn't work though, as refs made with useRef are not guaranteed to set on first render. So I read somewhere I need to use useCallback instead, but I get an error because I have the null
issue I explained above.
P.S.: Someone in the chat asked why I'm using node.querySelector
, I'll explain here as well in case it's someone else's question too: AG Grid makes two "boxes" to display the grid, one that is a container of the grid and wraps it, and another one which are the actual rows. If the grid does not have many rows, the wrapping one will have extra white space around the grid rows. So if I use node itself, those extra white space will be considered too, i.e., clicking on those white space will not cause deselection.