# @martinstark/use-once

> Roughly a react hook equivalent of componentWillMount

Latest version **1.0.4** (published 2021-04-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install @martinstark/use-once
pnpm add @martinstark/use-once
yarn add @martinstark/use-once
bun add @martinstark/use-once
```

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2021-04-29 |
| First published | 2021-04-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 6.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Maintainers | martinstark |
| Keywords | hook, react, componentWillMount, typescript |

## Links

- npm: https://www.npmjs.com/package/@martinstark/use-once
- Repository: https://github.com/martinstark/useOnce
- Homepage: https://github.com/martinstark/useOnce#readme
- Issues: https://github.com/martinstark/useOnce/issues
- npm.io page: https://npm.io/package/@martinstark/use-once

## 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.4 (latest) — 2021-04-29
- 1.0.3 — 2021-04-29
- 1.0.2 — 2021-04-29
- 1.0.1 — 2021-04-29

## README

# useOnce

Tiny typescript hook for running a segment of code once, immediately before the render is committed to the screen.

It is the rough equivalent of `componentWillMount` using React hooks.

See [explanation](https://dev.to/video/the-equivalent-of-componentwillmount-using-react-hooks-11em).

_Note: this is not necessarily a recommended pattern, but it is a question that comes up every so often._

# Usage

```javascript
// Without return value

const runBeforeFirstRender = () => {};

const Component = () => {
  useOnce(runBeforeFirstRender);

  return <></>;
};

// With return value

const runBeforeFirstRender = () => "result";

const Component = () => {
  const r = useOnce(runBeforeFirstRender);

  return <>{r}</>;
};
```

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