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

22 lines
479 B
TypeScript

import type { Ref } from 'vue';
import { 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, (next, pre) => {
if (shouldUpdate) {
if (shouldUpdate(next, pre)) {
cacheRef.value = getValue();
}
} else {
cacheRef.value = getValue();
}
});
return cacheRef;
}