A React hook that stores a value from the previous render
npm install @react-hook/previousnpm i @react-hook/previous
A React hook that stores a value from the previous render.
``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]
}
`
`ts`
const usePrevious:
value: T,
initialValue?: T | undefined
) => T | undefined
| Argument | Type | Default | Required? | Description |
| ------------ | --------------- | ----------- | --------- | ------------------------------------------------------------------------- |
| value | T | | Yes | The current value |T | undefined
| initialValue | | undefined` | No | The value returned by the hook when the current value has not yet changed |
MIT