use-true-element-size
TypeScript icon, indicating that this package has built-in type declarations

1.1.1 • Public • Published

use-true-element-size

A simple and versatile use hook to track for the DOM-element sizes (width and height)

NPM JavaScript Style Guide

A hook that returns the current width and height of the DOM element. The main benefit that hook doesn't use window resize event and can be useful in cases of element resizing without window resizing.

Quick start

JavaScript:

import { useRef } from 'react';
import { useTrueElementSize } from 'use-true-element-size';

const Example = () => {
  const divRef = useRef(null);
  const { elementWidth, elementHeight } = useTrueElementSize(divRef);

  return (
    <div ref={divRef}>
      width: ${elementWidth}
      height: ${elementHeight}
    </div>
  );
};

TypeScript:

import { useRef } from 'react';
import { useTrueElementSize } from 'use-true-element-size';

const Example = () => {
  const divRef = useRef<HTMLDivElement>(null);
  const { elementWidth, elementHeight } = useTrueElementSize(divRef);

  return (
    <div ref={divRef}>
      width: ${elementWidth}
      height: ${elementHeight}
    </div>
  );
};

Install

npm:

npm install use-true-element-size

yarn:

yarn add use-true-element-size

API

Options

Type Description
elementRef RefObject<HTMLElement> Ref object assigned to the HTML element to track its size

Returns {elementWidth: number, elementHeight: number}

Type Description
elementWidth number Width of the element
elementHeight centered Height of the element

License

MIT © [https://github.com/onceup](https://github.com/https://github.com/onceup)

/use-true-element-size/

    Package Sidebar

    Install

    npm i use-true-element-size

    Weekly Downloads

    64

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    11.5 kB

    Total Files

    13

    Last publish

    Collaborators

    • onceup