Created
October 25, 2019 20:52
-
-
Save sanusart/a7f063acb09f2306ae83be70456bb5ba to your computer and use it in GitHub Desktop.
React portal component #reactjs #react #portal #hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect } from 'react'; | |
import ReactDOM from 'react-dom'; | |
const portalTarget = document.getElementById('portal'); | |
const Portal = ({ children }) => { | |
const element = document.createElement('div'); | |
useEffect(() => { | |
portalTarget.appendChild(element); | |
return () => portalTarget.removeChild(element); | |
}); | |
return ReactDOM.createPortal(children, element); | |
}; | |
export default Portal; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment