Skip to main content

usePrevious

// Implement a usePrevious hook that returns the previous value of a state.

/**
* @param {*} state
* @returns {*}
*/

import {useRef} from 'react';

export default function usePrevious(value) {
const currentRef = useRef(value);
const previousRef = useRef();

if (currentRef.current !== value) {
previousRef.current = currentRef.current;
currentRef.current = value;
}

return previousRef.current;
}