# @rooks/use-mutation-observer

> A React Hooks package for mutation-observer

Latest version **4.11.2** (published 2021-05-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @rooks/use-mutation-observer
pnpm add @rooks/use-mutation-observer
yarn add @rooks/use-mutation-observer
bun add @rooks/use-mutation-observer
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.11.2 |
| Published | 2021-05-01 |
| First published | 2018-11-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 10 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3451 |
| Maintainers | imbhargav5 |
| Keywords | use, react-hooks.org, react, hooks, rooks, mutation, mutation observer, mutation observer hook |

## Links

- npm: https://www.npmjs.com/package/@rooks/use-mutation-observer
- Repository: https://github.com/imbhargav5/rooks
- Homepage: https://react-hooks.org/docs/use-mutation-observer
- Issues: https://github.com/imbhargav5/rooks/issues
- npm.io page: https://npm.io/package/@rooks/use-mutation-observer

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 4.11.2 (latest) — 2021-05-01
- 4.9.0-canary.0 (canary) — 2021-02-10
- 3.4.4-71d48a96.0 (next) — 2019-12-14
- 3.4.2-0668aca8.0 (dev) — 2019-12-04
- 3.3.0-alpha.0 (alpha) — 2019-09-07
- 3.0.0-beta.4 (beta) — 2019-04-14
- 1.0.11-alpha.1 (remove-readme) — 2019-01-27
- 4.11.1 — 2021-05-01
- 4.11.0 — 2021-04-06
- 4.10.1 — 2021-03-31
- 4.10.0 — 2021-03-21
- 4.9.2 — 2021-02-21
- 4.9.1 — 2021-02-11
- 4.9.0 — 2021-02-10
- 4.8.1 — 2021-02-03
- … 105 more at https://npm.io/package/@rooks/use-mutation-observer/versions

## README

# @rooks/use-mutation-observer

## *Note: Future updates to this package have moved to the main package* [rooks](https://npmjs.com/package/rooks). All hooks now reside in a single package which you can install using

```
npm install rooks
```

or 

```
yarn add rooks
```

Rooks is completely treeshakeable and if you use only 1 of the 50+ hooks in the package, only that hook will be bundled with your code. Your bundle will only contain the hooks that you need. Cheers!

![TitleCard](https://raw.githubusercontent.com/imbhargav5/rooks/v4-compat/packages/mutation-observer/title-card.svg)

![Build Status](https://github.com/imbhargav5/rooks/workflows/Node%20CI/badge.svg)![](https://img.shields.io/npm/v/@rooks/use-mutation-observer/latest.svg) ![](https://img.shields.io/npm/l/@rooks/use-mutation-observer.svg) ![](https://img.shields.io/npm/dt/@rooks/use-mutation-observer.svg) ![](https://img.shields.io/david/imbhargav5/rooks.svg?path=packages%2Fmutation-observer)




## About 
Mutation Observer hook for React.
<br/>

## Installation

```
npm install --save @rooks/use-mutation-observer
```

## Importing the hook

```javascript
import useMutationObserver from "@rooks/use-mutation-observer"
```


## Usage

```jsx
function Demo() {
  const myRef = useRef();
  const [mutationCount, setMutationCount] = useState(0);
  const incrementMutationCount = () => {
    return setMutationCount(mutationCount + 1);
  };
  useMutationObserver(myRef, incrementMutationCount);
  const [XOffset, setXOffset] = useState(0);
  const [YOffset, setYOffset] = useState(300);
  return (
    <>
      <div
        style={{
          width: 300,
          background: "lightblue",
          padding: "10px",
          position: "absolute",
          left: XOffset,
          top: YOffset
        }}
        ref={myRef}
      >
        <div
          style={{
            resize: "both",
            overflow: "auto",
            background: "white",
            color: "blue",
            maxWidth: "100%"
          }}
        >
          <p>
            Resize this div as you see fit. To demonstrate that it also updates
            on child dom nodes resize
          </p>
        </div>
        <h2>Bounds</h2>
        <p>
          <button onClick={() => setXOffset(XOffset - 5)}> Move Left </button>
          <button onClick={() => setXOffset(XOffset + 5)}> Move Right </button>
        </p>
        <p>
          <button onClick={() => setYOffset(YOffset - 5)}> Move Up </button>
          <button onClick={() => setYOffset(YOffset + 5)}> Move Down </button>
        </p>
      </div>
      <div style={{ height: 500 }} onClick={incrementMutationCount}>
        <pre>Mutation count {mutationCount}</pre>
      </div>
    </>
  );
}

render(<Demo/>)
```

### Arguments

| Argument | Type      | Description                                                                                       | Default value                                                           |
| -------- | --------- | ------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------- |
| ref      | React ref | Ref which should be observed for Mutations                                                        | undefined                                                               |
| callback | function  | Function which should be invoked on mutation. It is called with the `mutationList` and `observer` | undefined                                                               |
| config   | object    | Mutation Observer configuration                                                                   | {attributes: true,,characterData: true,,subtree: true,,childList: true} |

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