Skip to content

Instantly share code, notes, and snippets.

@moeriki
Created August 31, 2023 09:50
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 moeriki/7d5f694c917a26e8cc4de1f7f218286e to your computer and use it in GitHub Desktop.
Save moeriki/7d5f694c917a26e8cc4de1f7f218286e to your computer and use it in GitHub Desktop.
export function useInnerRef<T>(forwardedRef: ForwardedRef<T>) {
const innerRef = useRef<T | null>(null);
const setInnerRef: RefCallback<T> = (element: T | null) => {
if (typeof forwardedRef === 'function') {
forwardedRef(element);
} else if (forwardedRef) {
forwardedRef.current = element;
}
innerRef.current = element;
};
return [innerRef, setInnerRef] as const;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment