# react-hook-on-mount

> A React hook to run an effect only on the first render

Latest version **1.0.2** (published 2022-06-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-hook-on-mount
pnpm add react-hook-on-mount
yarn add react-hook-on-mount
bun add react-hook-on-mount
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-06-04 |
| First published | 2022-06-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Bence A. Toth |
| Maintainers | bence.a.toth |
| Keywords | react, hook, mount, effect |

## Links

- npm: https://www.npmjs.com/package/react-hook-on-mount
- Repository: https://github.com/bence-toth/react-hook-on-mount
- Homepage: https://github.com/bence-toth/react-hook-on-mount#readme
- Issues: https://github.com/bence-toth/react-hook-on-mount/issues
- npm.io page: https://npm.io/package/react-hook-on-mount

## 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

- 1.0.2 (latest) — 2022-06-04
- 1.0.1 — 2022-06-04
- 1.0.0 — 2022-06-04

## README

# react-hook-on-mount 🏆

A React hook to run an effect only on the first render.

## Installation

Using `npm`:

```sh
npm install --save react-hook-on-mount
```

Using `yarn`:

```sh
yarn add react-hook-on-mount
```

## Usage

```jsx
import { useState } from "react";
import useOnMount from "react-hook-on-mount";

const ComponentWithOnMount = () => {
  const [inspiration, setInspiration] = useState();

  useOnMount(() => {
    fetch("https://api.goprogram.ai/inspiration")
      .then((result) => result.json())
      .then(setInspiration);
  });

  if (!inspiration) {
    return null;
  }

  return (
    <figure>
      <blockquote>{inspiration.quote}</blockquote>
      <figcaption>— {inspiration.author}</figcaption>
    </figure>
  );
};
```

## Motivation

In React v18 [a new strict mode behaviour was introduced](https://reactjs.org/blog/2022/03/29/react-v18.html#new-strict-mode-behaviors), which in development mode will automatically unmount and remount every component, whenever a component mounts for the first time, restoring the previous state on the second mount.

In development mode, this will result in components using the `useEffect` hook like this to run the effect twice when the component mounts:

```jsx
const ComponentWithOnMountEffect = () => {
  useEffect(() => {
    fetch("https://api.goprogram.ai/inspiration")
      .then(
        // ...
      )
  });

  return (
    // ...
  );
};
```

To avoid this, components using the `useEffect` hook to run an effect only once when the component mounts are recommended to use the `useOnMount` hook instead, which will, in addition to effect the hook on the first mount, will make sure that the effect never runs again.

## Contributions

Contributions are welcome. File bug reports, create pull requests, feel free to reach out at tothab@gmail.com.

## Licence

[MIT](./LICENSE)

---
_Source: https://npm.io/package/react-hook-on-mount · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
