UNPKG

ahooks

Version:
13 lines (12 loc) 416 B
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;