Skip to content

Instantly share code, notes, and snippets.

@andycarrell
Created January 16, 2023 18:36
Show Gist options
  • Save andycarrell/c7729318d2adb6e5cb494228abe313d1 to your computer and use it in GitHub Desktop.
Save andycarrell/c7729318d2adb6e5cb494228abe313d1 to your computer and use it in GitHub Desktop.
function useHTMLContainer() {
const container = useRef<HTMLDivElement | null>(null);
useEffect(() => {
const element = document.createElement('div');
document.body.appendChild(element);
container.current = element;
return () => {
if (container.current) {
document.body.removeChild(container.current);
container.current = null;
}
};
}, []);
return container.current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment