ahooks
Version:
react hooks library
13 lines (12 loc) • 416 B
JavaScript
import { useRef } from 'react';
const defaultShouldUpdate = (a, b) => !Object.is(a, b);
function usePrevious(state, shouldUpdate = defaultShouldUpdate) {
const prevRef = useRef(undefined);
const curRef = useRef(undefined);
if (shouldUpdate(curRef.current, state)) {
prevRef.current = curRef.current;
curRef.current = state;
}
return prevRef.current;
}
export default usePrevious;