React Recipes

The `usePrevious` hook — remembering previous values

This hook lets us remember the previous value of things — props, state, or any other value.

Since it's a common use-case, the React docs mention this hook may come as part of a next version of React.

Ingredients

Implementation

function usePrevious(value) {
let ref = useRef();
useEffect(() => {
ref.current = value;
});
return ref.current;
}

Explanation