perf: useRefs函数接收泛型类型

pull/30/MERGE
xingyu 2023-08-11 20:05:24 +08:00
parent 7aeba3367d
commit ee56231f02
1 changed files with 8 additions and 4 deletions

View File

@ -1,16 +1,20 @@
import type { Ref } from 'vue' import type { Ref } from 'vue'
import { onBeforeUpdate, ref } from 'vue' import { onBeforeUpdate, ref } from 'vue'
export function useRefs(): [Ref<HTMLElement[]>, (index: number) => (el: HTMLElement) => void] { function useRefs<T = HTMLElement>(): { refs: Ref<T[]>; setRefs: (index: number) => (el: T) => void } {
const refs = ref([]) as Ref<HTMLElement[]> const refs = ref([]) as Ref<T[]>
onBeforeUpdate(() => { onBeforeUpdate(() => {
refs.value = [] refs.value = []
}) })
const setRefs = (index: number) => (el: HTMLElement) => { const setRefs = (index: number) => (el: T) => {
refs.value[index] = el refs.value[index] = el
} }
return [refs, setRefs] return {
refs,
setRefs,
}
} }
export { useRefs }