Fix Portal
This commit is contained in:
parent
5980d6bff6
commit
94cb909700
1 changed files with 27 additions and 9 deletions
|
|
@ -1,7 +1,10 @@
|
||||||
import { useEffect } from 'react'
|
import { useEffect, useRef, useState } from 'react'
|
||||||
import { createPortal } from 'react-dom'
|
import { createPortal, render } from 'react-dom'
|
||||||
|
|
||||||
const Portal = ({ children }) => {
|
const Portal = ({ children }) => {
|
||||||
|
const el = document.createElement('div')
|
||||||
|
el.classList.add('modal_content')
|
||||||
|
|
||||||
let modalRoot = document.getElementById('modal')
|
let modalRoot = document.getElementById('modal')
|
||||||
|
|
||||||
if (!modalRoot) {
|
if (!modalRoot) {
|
||||||
|
|
@ -10,14 +13,29 @@ const Portal = ({ children }) => {
|
||||||
document.body.appendChild(modalRoot)
|
document.body.appendChild(modalRoot)
|
||||||
}
|
}
|
||||||
|
|
||||||
const modalElement = document.createElement('div')
|
const [val, setVal] = useState(true)
|
||||||
|
|
||||||
|
const modalDiv = useRef(null)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
modalRoot.appendChild(modalElement)
|
if (!modalDiv.current) {
|
||||||
return () => modalRoot.removeChild(modalElement)
|
modalDiv.current = el
|
||||||
})
|
setVal(!val)
|
||||||
|
}
|
||||||
|
}, [modalDiv])
|
||||||
|
|
||||||
return createPortal(children, modalElement)
|
useEffect(() => {
|
||||||
|
modalRoot.appendChild(modalDiv.current)
|
||||||
|
return () => {
|
||||||
|
return modalRoot.removeChild(modalDiv.current)
|
||||||
|
}
|
||||||
|
}, [modalDiv, modalRoot])
|
||||||
|
|
||||||
|
if (modalDiv.current) {
|
||||||
|
return createPortal(children, modalDiv.current)
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Portal
|
export default Portal
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue