Medium
You're building a modal component that needs to render outside the main app container. Why is this implementation causing the modal to disappear unexpectedly?
1const Modal = ({ isOpen, children }) => {2 const modalRoot = document.getElementById('modal-root');3 const modalElement = document.createElement('div');45 useEffect(() => {6 if (isOpen) {7 modalRoot.appendChild(modalElement);8 }9 return () => {10 modalRoot.removeChild(modalElement);11 };12 }, [isOpen]);1314 return isOpen ? createPortal(children, modalElement) : null;15};