@react-hook/previous
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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

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()

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

Package Sidebar

Install

npm i @react-hook/previous

Weekly Downloads

4,850

Version

1.0.1

License

MIT

Unpacked Size

13.1 kB

Total Files

22

Last publish

Collaborators

  • jaredlunde