Since I do not understand React and HTML rendering behavior entirely, I'm unsure is there a potential issue with React refs being potentially null if they are accessed from function wrapped with window.requestAnimationFrame, that in turn is called from event listener that component subscribes on mount and un-subscribes on unmount. I present a small code snippet that better illustrates the logical uncertainty I face:
export const MyComponent: React.FunctionComponent = () => {
const divRef = useRef<HTMLDivElement | null>(null);
const doSomethingWithRef = (): void => {
// This is my uncertainty:
// Can this ever be called AFTER unmount has happened, so that divRef.current is null?
console.log(["divRef", divRef.current]);
};
const handleWindowResizeEvent = (): void => {
window.requestAnimationFrame(() => {
doSomethingWithRef();
});
};
useEffect(
() => {
window.addEventListender("resize", handleWindowResizeEvent);
// componentWillUnmount
return (): void => {
window.removeEventListender("resize", handleWindowResizeEvent);
};
},
[] // componentDidMount
};
return <div ref={divRef}>Hello, World!</div>;
};