client: create Portal inside "app" root

This commit is contained in:
Jesse Chan
2021-03-07 22:32:13 +08:00
parent 227c48adfa
commit f8c671fc9b
+12 -2
View File
@@ -11,12 +11,22 @@ const Portal: FC<PortalProps> = ({children}: PortalProps) => {
useEffect(() => {
mountPoint.current = document.createElement('div');
mountPoint.current.classList.add('portal');
document.body.appendChild(mountPoint.current);
const appElement = document.getElementById('app');
if (appElement == null) {
document.body.appendChild(mountPoint.current);
} else {
appElement.appendChild(mountPoint.current);
}
return () => {
if (mountPoint.current != null) {
ReactDOM.unmountComponentAtNode(mountPoint.current);
document.body.removeChild(mountPoint.current);
if (appElement == null) {
document.body.removeChild(mountPoint.current);
} else {
appElement.removeChild(mountPoint.current);
}
}
};
}, []);