Skip to content

Instantly share code, notes, and snippets.

@christiannaths
Created April 21, 2023 05:15
Show Gist options
  • Save christiannaths/e34b25a664270a3da8defb7f9ddb8638 to your computer and use it in GitHub Desktop.
Save christiannaths/e34b25a664270a3da8defb7f9ddb8638 to your computer and use it in GitHub Desktop.
function useElementSize(): [
(el: HTMLElement | null) => void,
{ width?: number; height?: number }
] {
const [el, ref] = React.useState(null);
const width = React.useSyncExternalStore(noop, () => el?.offsetWidth);
const height = React.useSyncExternalStore(noop, () => el?.offsetHeight);
return [ref, { width, height }];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment