ant-design-vue/components/_util/hooks/useMemo.ts

17 lines
367 B
TypeScript
Raw Normal View History

2021-06-22 13:26:19 +08:00
import { Ref, ref, watch } from 'vue';
export default function useMemo<T>(
getValue: () => T,
condition: any[],
shouldUpdate: (prev: any[], next: any[]) => boolean,
) {
const cacheRef: Ref<T> = ref(getValue() as any);
watch(condition, (pre, next) => {
if (shouldUpdate(pre, next)) {
cacheRef.value = getValue();
}
});
return cacheRef;
}