Skip to main content

usePrevious

React state hook that returns the previous state as described in the React hooks FAQ.

API

function usePrevious<GenericType>(state: GenericType): GenericType | undefined;

Usage

import { usePrevious } from '@feedzai/js-utilities/hooks';

function Component() {
const [value, setValue] = useState(0);
const previousValue = usePrevious(value);
return (
<div>
<span>{previousValue}</span>
<button onClick={() => setValue((prev) => prev + 1)}>Increment</button>
</div>
);
}