raddix

usePrevious

Returns previous value of given state.

Installation

Install the custom hook from your command line.

npm i @raddix/use-previous

Usage

import { useState } from 'react';
import { usePrevious } from '@raddix/use-previous';
export default function App() {
const [count, setCount] = useState(0)
const previousCount = usePrevious(count);
return (
<div>
<p>Current value: {count}</p>
<p>Previous value: {previousCount}</p>
<button onClick={() => setCount(count + 1)}>
Increment Count
</button>
</div>
)
}

API

Parameters

NameDescriptionRequiredType
valueThe value of the state.Yesany

Returns

NameDescriptionType
previousValueThe previous value of given state.any