Skip to main content

usePrevious

A custom hook that returns the value of the argument from the previous render.


Interface

const usePrevious: <T>(value: T) => T;

Usage

import { usePrevious } from '@devgrace/react';
import { useState } from 'react';

const Example = () => {
const [count, setCount] = useState(0);
const previousCount = usePrevious(count);

const onIncrementCount = () => {
setCount(count + 1);
};

return (
<div>
<div>Current Count is - {count}</div>
<div>Previous Count is - {previousCount}</div>
<button onClick={onIncrementCount}>Increment</button>
</div>
);
};
Current Count is - 0
Previous Count is - 0