Created
February 16, 2023 19:33
-
-
Save mendes5/5d190ead0474a04e68af5d70dffec6d6 to your computer and use it in GitHub Desktop.
React portals as they were in my head before learning how createPortal actually works
This file contains hidden or 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 { render } from "react-dom"; | |
import { atom, useAtom } from "jotai"; | |
import { useEffect, useState } from "react"; | |
const portalGun = () => { | |
const Children = atom(null); | |
const EntryPortal = ({ children }) => { | |
const [, setChildren] = useAtom(Children); | |
useEffect(() => { | |
setChildren(children); | |
}, [setChildren, children]); | |
return null; | |
}; | |
const ExitPortal = () => { | |
const [children] = useAtom(Children); | |
return <>{children || null}</>; | |
}; | |
return [EntryPortal, ExitPortal]; | |
}; | |
const [Orange, Blue] = portalGun(); | |
const App = () => ( | |
<div className="App"> | |
<Blue /> | |
Boundary | |
<Orange>XXXXXX</Orange> | |
</div> | |
); | |
render(<App />, document.getElementById("root")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment