How can I wait for ref to be ready but also only work inside my use effect with condition?
Here is what I want to do:
I want to select all rows in my DataGrid component using ref when popup is visible.
This code won't work, because I don't have ref current instance at that time.
useEffect(() => {
if (isVisible) {
dataGridRef.current.instance.selectAll()
}
}, [isVisible])
So I searched and found useCallback to update when ref is connected.
const dataGridRef = useCallback(node => {
if (node !== null) {
node.instance.selectAll()
}
}, [])
<DataGrid ref={dataGridRef} ..
But this time, I cannot use ref instance when I have to reset(unselect data grid using ref)
Your callback ref will capture when changes are made to the node, and in that same function execution, you can update a ref object to be referenced outside the callback at your whims.
Here is an example CodeSandbox of the above concepts.