I am trying to create a Modal on React and am using TypeScript. I got an error of a 'parameter of type Node' and couldn't figure out what this is (nor subsequently fix my problem).
const Modal = ({ children }: { children: any }) => {
const elRef = useRef<HTMLDivElement | null>(null); //tried adding'| Node'
if (!elRef.current) {
const div = document.createElement("div");
div.className = "modalDiv";
elRef.current = div;
}
useEffect(() => {
const modalRoot = document.getElementById("modal");
//Argument of type 'HTMLDivElement | null' is not assignable to parameter of type 'Node'.
//Type 'null' is not assignable to type 'Node'.ts(2345)
modalRoot!.appendChild(elRef.current);
return () => modalRoot!.removeChild(elRef.current);
}, []);
return createPortal(<div>{children}</div>, elRef.current);
};