SHARE
TWEET

Untitled

a guest Oct 21st, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3.  
  4. function Portal({ children }: React.PropsWithChildren<{}>) {
  5.   const [container, setContainer] = React.useState<Element>()
  6.  
  7.   React.useLayoutEffect(() => {
  8.     const el = document.createElement('div')
  9.     el.className = 'portal'
  10.     document.body.append(el)
  11.     setContainer(el)
  12.  
  13.     return () => el.remove()
  14.   }, [])
  15.  
  16.   return container
  17.     ? ReactDOM.createPortal(children, container)
  18.     : null
  19. }
  20.  
  21. export default Portal
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top