I've got the "Object is possibly null" error many times and usually I use a safety "if statement" in case it returns null.
I've got the following function:
const ModalOverlay = (props: any[]) => {
const overlayEl = useRef(null);
useEffect(() => {
overlayEl.current.focus();
});
return <div {...props} ref={overlayEl} />;
}
But overlayEl.current
gets the error "Object is not defined". So I've tried:
if (!overlayEl) {
return null
} else {
useEffect(() => {
overlayEl.current.focus();
});
return <div {...props} ref={overlayEl} />;
}
Which didn't work. I've tried also:
overlay && overlayEl.current.focus();
Any hints would be highly appreciated! Thanks