Open shfshanyue opened 3 years ago
import { useRef } from "react";
type ShouldUpdateFunc<T> = (prev: T | undefined, next: T) => boolean;
const defalutShouldUpdate = <T>(prev?: T, next?: T) => prev !== next;
function usePrevious<T>(
state: T,
shouldUpdateFun: ShouldUpdateFunc<T> = defalutShouldUpdate
): T | undefined {
const prev = useRef<T>();
const cur = useRef<T>();
if (shouldUpdateFun(cur.current, state)) {
prev.current = cur.current;
cur.current = state;
}
return prev.current;
}
export default usePrevious;
ahook的实现版本 很简洁
TODO