Skip to content

Instantly share code, notes, and snippets.

@mendes5
Created February 16, 2023 19:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mendes5/5d190ead0474a04e68af5d70dffec6d6 to your computer and use it in GitHub Desktop.
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
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