Skip to content

akshitkrnagpal/use-size-performant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

use-size-performant

React hook to retrieve the size of an element

NPM

Installation

npm i use-size-performant
// or
yarn add use-size-performant

Usage

Using Ref

import useSize from "use-size-performant";
const YourComponent = () => {
  const ref = useRef(null);
  const { height, width } = useSize(ref);
  return (
    <div ref={ref}>
      <p>Height: {height}</p>
      <p>Width: {width}</p>
    </div>
  );
};

Using DOM elements

import useSize from "use-size-performant";
const YourComponent = () => {
  const dom = document.querySelector('body');
  const { height, width } = useSize(dom);
  return (
    <div>
      <p>Height: {height}</p>
      <p>Width: {width}</p>
    </div>
  );
};

License

MIT © Akshit Kr Nagpal