https://blog.csdn.net/sd19871122/article/details/97612107
https://blog.csdn.net/mmzzll2019/article/details/89348085?utm_medium=distribute.pc_relevant.none-task-blog-title-3&spm=1001.2101.3001.4242
createProtal
改造 Modal 组件
在 html 中除了 div#root
之外,给 Modal 预留了一个新的 div#modal-root
,:
-
const appRoot = document.getElementById(‘root‘);
-
const modalRoot = document.getElementById(‘modal-root‘);
改造 Modal 容器
新的 Modal 容器组件内容如下:
-
class ModalContainer extends Component {
-
-
-
this.el = document.createElement(‘div‘);
-
-
-
modalRoot.appendChild(this.el);
-
-
-
modalRoot.removeChild(this.el);
-
-
-
return ReactDOM.createPortal(
-
-
-
-
-
}
将 ModalContent 挂载到 ModalContainer
-
class App2 extends Component {
-
-
-
-
-
-
-
-
-
name: ‘clickme‘ + Date.now()
-
-
-
-
-
-
-
-
-
-
-
-
}
即使主组件是 overflow:hidden
的,但是 modal 能够正常出现,同时,因为 div#modal-root
是 z-index: 999
,因此会置于最上层。
React.createPortal()
原文:https://www.cnblogs.com/zwjun/p/13718562.html