I am trying to use useRef and useCallback together but my useRef is not updating sometime, it seems. It is actually weird and it happens only sometimes. Even if I use normal ref operating, it will behave the same.
function useRefCallback() {
const ref = useRef(null);
const setRef = useCallback(node => {
ref.current = node;
}, []);
return [ref, setRef];
}
function MapWidget({ options }) {
const [mapWidgetRef, setMapWidgetRef] = useRefCallback();
// I try to pass to another hook to get width and height of that mapWidgetRef.
const [mapWidgetWidth, mapWidgetHeight] = useResizeObserver(mapWidgetRef);
return (
<div ref={setMapWidgetRef}>
...
</div>
}
If I try to print out mapWidgetRef
I would get just object
sometimes which is not good and it will make mapWidgetWidth, mapWidgetHeight
undefined. Most of the time is okay and its working as expected.
Not Good
Good
How can I solve this?