use-refs.mjs 374 B

1234567891011121314151617181920
  1. import { ref, onBeforeUpdate } from "vue";
  2. function useRefs() {
  3. const refs = ref([]);
  4. const cache = [];
  5. onBeforeUpdate(() => {
  6. refs.value = [];
  7. });
  8. const setRefs = (index) => {
  9. if (!cache[index]) {
  10. cache[index] = (el) => {
  11. refs.value[index] = el;
  12. };
  13. }
  14. return cache[index];
  15. };
  16. return [refs, setRefs];
  17. }
  18. export {
  19. useRefs
  20. };