--
Open the terminal in your project's root folder and run the following command:
npm install use-size-hook --save
Import the hook on top of any component you want to track the size of.
import useSize from 'use-size-hook';
Call the hook inside the component. The hook returns an array of two elements:
target
is a reference to the dom, you should pass it asaref
prop to the dom element of your component you want to track the size of.currentSize
is an object containing the currentwidth
andheight
of the element you are tracking. Measurement is in pixels.
import React from 'react';
import useSize from 'use-size-hook';
const TestComponent = () => {
const [target, currentSize] = useSize();
return (
<div ref={target}>
Height: {currentSize.height} - Width: {currentSize.with}
</div>
);
};
export default TestComponent;