Skip to content

Latest commit

 

History

History
 
 

previous

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

usePrevious()

Bundlephobia Types Build status 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 The value returned by the hook when the current value has not yet changed

LICENSE

MIT