mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-06 03:57:59 +08:00
perf: caching handlers of useRefs (#10108)
This commit is contained in:
parent
7e4d3da983
commit
8d60697ec1
@ -2,13 +2,19 @@ import { ref, Ref, onBeforeUpdate } from 'vue';
|
||||
|
||||
export function useRefs<T = Element>() {
|
||||
const refs = ref([]) as Ref<T[]>;
|
||||
const cache: Array<(el: unknown) => void> = [];
|
||||
|
||||
onBeforeUpdate(() => {
|
||||
refs.value = [];
|
||||
});
|
||||
|
||||
const setRefs = (index: number) => (el: unknown) => {
|
||||
refs.value[index] = el as T;
|
||||
const setRefs = (index: number) => {
|
||||
if (!cache[index]) {
|
||||
cache[index] = (el: unknown) => {
|
||||
refs.value[index] = el as T;
|
||||
};
|
||||
}
|
||||
return cache[index];
|
||||
};
|
||||
|
||||
return [refs, setRefs] as const;
|
||||
|
Loading…
x
Reference in New Issue
Block a user