Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@react-hook/previous

jaredLunde32.2kMIT1.0.1TypeScript support: included

A React hook that stores a value from the previous render

react, use-previous, use-previous-value, react hook, react previous hook, react hook previous, react hook previous value

readme


usePrevious()

Bundlephobia Types NPM Version MIT License

npm i @react-hook/previous

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

LICENSE

MIT