Skip to content

Commit

Permalink
feat: resize observer hook
Browse files Browse the repository at this point in the history
  • Loading branch information
zstix committed Sep 8, 2020
1 parent fcf4ed1 commit 564d647
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/hooks/useResizeObserver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useLayoutEffect, useState, useRef } from 'react';

const useResizeObserver = () => {
const ref = useRef(null);
const [height, setHeight] = useState(0);

const resizeObserver = new ResizeObserver((entries) => {
entries.forEach((entry) => {
setHeight(entry.contentBoxSize.blockSize);
});
});

useLayoutEffect(() => {
resizeObserver.observe(ref.current);
}, []);

return [ref, height];
};

export default useResizeObserver;

0 comments on commit 564d647

Please sign in to comment.