1.0.2 • Published 5 years ago
use-size-hook v1.0.2
use-size-hook
A simple custom hook to keep track of a component's width and height;
--
Installation
Open the terminal in your project's root folder and run the following command:
npm install use-size-hook --saveUsage
1 Import the hook
Import the hook on top of any component you want to track the size of.
import useSize from 'use-size-hook';2 Destructure target and size
Call the hook inside the component. The hook returns an array of two elements:
targetis a reference to the dom, you should pass it asarefprop to the dom element of your component you want to track the size of.currentSizeis an object containing the currentwidthandheightof 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;