# @talves/use-component-size

> React hook (useComponentSize)

Latest version **0.3.1** (published 2021-03-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @talves/use-component-size
pnpm add @talves/use-component-size
yarn add @talves/use-component-size
bun add @talves/use-component-size
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.1 |
| Published | 2021-03-10 |
| First published | 2019-08-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 12.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | talves |
| Maintainers | talves |

## Links

- npm: https://www.npmjs.com/package/@talves/use-component-size
- Repository: https://github.com/talves/react-use-hooks/packages/@talves/use-component-size
- npm.io page: https://npm.io/package/@talves/use-component-size

## Dependencies (3)

- [react](https://npm.io/package/react.md) ^17.0.1
- [react-dom](https://npm.io/package/react-dom.md) ^17.0.1
- [resize-observer-polyfill](https://npm.io/package/resize-observer-polyfill.md) ^1.5.1

## Recent versions

- 0.3.1 (latest) — 2021-03-10
- 0.3.0 — 2021-02-19
- 0.2.1 — 2021-02-16
- 0.2.0 — 2021-02-12
- 0.1.1 — 2021-02-09
- 0.1.0 — 2021-02-09
- 0.0.2 — 2019-08-22
- 0.0.1 — 2019-08-22

## README

<div align="center">
<h1>@talves/use-component-size</h1>
<p>React hook (usePrevious)</p>
</div>

<hr />

## Installation

This module is distributed via [npm][npm] which is bundled with [node][node] and
should be installed as one of your project's `dependencies`:

```
npm install --save @talves/use-component-size
```

OR

```
yarn add @talves/use-component-size
```

## Usage

```js
/* using react ^17.0.1 */
// Creates a component that runs onSizeChange of the component
// only when the component size changed, give current and previous
import { useRef, useCallback, useEffect } from "react";
import { useComponentSize } from "@talves/use-component-size";
import { usePrevious } from "@talves/use-previous";

function sizeEqual(a, b) {
  return JSON.stringify(a) === JSON.stringify(b);
}

const SizeWrapper = ({ children, onSizeChange, ...props }) => {
  const wrapperRef = useRef(null);
  const componentSize = useComponentSize(wrapperRef); // A custom Hook
  const previousSize = usePrevious(componentSize);

  const handleSizeChange = useCallback(
    (size) => {
      if (typeof onSizeChange === "function") {
        onSizeChange(size);
      }
    },
    [onSizeChange]
  );
  useEffect(() => {
    const notEqual = !sizeEqual(componentSize, previousSize);
    if (notEqual) {
      handleSizeChange({
        current: componentSize,
        previous: previousSize,
      });
    }
  }, [componentSize, previousSize, handleSizeChange]);

  return (
    <div ref={wrapperRef} {...props}>
      {children}
    </div>
  );
};

export default SizeWrapper;
```

NOTE: This is a `module` package library for react with jsx. The choice is to resolve the export of main to `src/index.js`. There is a commonjs version in `dist/cjs/index.js` if someone needed.

## Other Solutions

Plenty of other usePrevious hooks out there.

[npm]: https://www.npmjs.com/
[node]: https://nodejs.org/

---
_Source: https://npm.io/package/@talves/use-component-size · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
