Skip to content

Commit

Permalink
[feat] Support customized ref in useDimensions (#2409)
Browse files Browse the repository at this point in the history
* [FEAT] Support customized ref in useDimensions

Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com>

* deps

Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com>

---------

Signed-off-by: Ihor Dykhta <dikhta.igor@gmail.com>
Co-authored-by: Rui Wang <64480766+albatross97@users.noreply.github.com>
  • Loading branch information
igorDykhta and albatross97 committed Oct 27, 2023
1 parent 4d72331 commit 79002ea
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/utils/src/observe-dimensions.ts
Expand Up @@ -95,9 +95,10 @@ function getSize(node, entry): Dimensions | null {
* @returns
*/
export default function useDimensions<T extends Element>(
nodeRef?: RefObject<T>,
throttleDelay = DEFAULT_THROTTLE_DELAY
): [RefObject<T>, Dimensions | null] {
const ref = useRef<T>(null);
const ref = nodeRef ?? useRef<T>(null);
const [size, setSize] = useState(null);

useEffect(() => {
Expand All @@ -124,7 +125,7 @@ export default function useDimensions<T extends Element>(
unobserveDimensions(current);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [throttleDelay]);
}, [throttleDelay, ref?.current]);

return [ref, size];
}

0 comments on commit 79002ea

Please sign in to comment.