Skip to main content

usePrevious

If you want to access the previous props or state in functional components, you can use the usePrevious hook. This hook would work for props, state, or any other calculated value.

import { usePrevious } from 'react-use-custom-hooks';

Usage example

const [value, setValue] = useState(initialState);
const previousValue = usePrevious(value);

Playground

Live Editor
Result
Loading...

API

function usePrevious<T>(value: T): T | undefined;

Params

PropertyDescriptionTypeDefault
valueThe state/prop value that previous value requiredany-