# @react-hook/previous

> A React hook that stores a value from the previous render

Latest version **1.0.1** (published 2020-06-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install @react-hook/previous
pnpm add @react-hook/previous
yarn add @react-hook/previous
bun add @react-hook/previous
```

## Health

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

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

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2020-06-05 |
| First published | 2020-06-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 12.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1529 |
| Author | Jared Lunde |
| Maintainers | jaredlunde |
| Keywords | react, use-previous, use-previous-value, react hook, react previous hook, react hook previous, react hook previous value |

## Links

- npm: https://www.npmjs.com/package/@react-hook/previous
- Repository: https://github.com/jaredLunde/react-hook
- Homepage: https://github.com/jaredLunde/react-hook/tree/master/packages/previous#readme
- Issues: https://github.com/jaredLunde/react-hook/issues
- npm.io page: https://npm.io/package/@react-hook/previous

## 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.1 (latest) — 2020-06-05
- 1.0.0 — 2020-06-05

## README

<hr>
<div align="center">
  <h1 align="center">
    usePrevious()
  </h1>
</div>

<p align="center">
  <a href="https://bundlephobia.com/result?p=@react-hook/previous">
    <img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@react-hook/previous?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Types" href="https://www.npmjs.com/package/@react-hook/previous">
    <img alt="Types" src="https://img.shields.io/npm/types/@react-hook/previous?style=for-the-badge&labelColor=24292e">
  </a>
  <!--
  <a aria-label="Code coverage report" href="https://codecov.io/gh/jaredLunde/react-hook">
    <img alt="Code coverage" src="https://img.shields.io/codecov/c/gh/jaredLunde/react-hook?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="Build status" href="https://travis-ci.com/jaredLunde/react-hook">
    <img alt="Build status" src="https://img.shields.io/travis/com/jaredLunde/react-hook?style=for-the-badge&labelColor=24292e">
  </a>
  -->
  <a aria-label="NPM version" href="https://www.npmjs.com/package/@react-hook/previous">
    <img alt="NPM Version" src="https://img.shields.io/npm/v/@react-hook/previous?style=for-the-badge&labelColor=24292e">
  </a>
  <a aria-label="License" href="https://jaredlunde.mit-license.org/">
    <img alt="MIT License" src="https://img.shields.io/npm/l/@react-hook/previous?style=for-the-badge&labelColor=24292e">
  </a>
</p>

<pre align="center">npm i @react-hook/previous</pre>
<hr>

A React hook that stores a value from the previous render.

## Quick Start

```jsx harmony
import * as React from 'react'
import usePrevious from '@react-hook/previous'

const useChanged = (onChange) => {
  const [status, setStatus] = React.useState('off')
  const prevStatus = usePrevious(status)

  React.useEffect(() => {
    if (status !== prevStatus) onChange()
  }, [status])

  return [status, setStatus]
}
```

## API

### usePrevious()

```ts
const usePrevious: <T extends any>(
  value: T,
  initialValue?: T | undefined
) => T | undefined
```

| Argument     | Type            | Default     | Required? | Description                                                               |
| ------------ | --------------- | ----------- | --------- | ------------------------------------------------------------------------- |
| value        | `T`             |             | Yes       | The current value                                                         |
| initialValue | `T | undefined` | `undefined` | No        | The value returned by the hook when the current value has not yet changed |

## LICENSE

MIT

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