2020-03-29 21:19:17 +00:00
|
|
|
import { useRef } from 'react';
|
2020-07-05 04:46:49 +00:00
|
|
|
import isEqual from 'react-fast-compare';
|
2020-03-29 21:19:17 +00:00
|
|
|
|
|
|
|
export const useDeepMemo = <T, K> (fn: () => T, key: K): T => {
|
|
|
|
const ref = useRef<{ key: K, value: T }>();
|
|
|
|
|
|
|
|
if (!ref.current || !isEqual(key, ref.current.key)) {
|
|
|
|
ref.current = { key, value: fn() };
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref.current.value;
|
|
|
|
};
|