--- title: Use useTemplateRef for Template Refs in Vue 3.5+ impact: MEDIUM impactDescription: Legacy ref pattern is error-prone due to name matching requirement type: best-practice tags: [vue3, vue35, template-refs, useTemplateRef, composition-api] --- # Use useTemplateRef for Template Refs in Vue 3.5+ **Impact: MEDIUM** - Before Vue 3.5, template refs required declaring a `ref()` with a name exactly matching the template's ref attribute. This fragile connection breaks silently during refactoring. Vue 3.5's `useTemplateRef()` eliminates this issue with explicit binding and better TypeScript support. The legacy pattern causes no errors or warnings when names don't match - the ref simply stays null, leading to confusing debugging sessions. ## Task Checklist - [ ] Use `useTemplateRef('ref-name')` in Vue 3.5+ projects - [ ] The first argument must exactly match the ref attribute value in the template - [ ] IDE support provides auto-completion for available ref names - [ ] TypeScript automatically infers the element type **Incorrect (Legacy Pattern):** ```vue ``` ```vue ``` **Correct (Vue 3.5+):** ```vue ``` ```vue ``` ## Limitation: v-for Refs `useTemplateRef()` does **NOT** work with `v-for` refs. You must use the legacy `ref()` pattern for collecting multiple element references in a loop. ```vue ``` **Why this limitation exists:** When using `v-for`, Vue populates the ref with an array of elements. The `useTemplateRef()` API was designed for single element references and does not support the array population mechanism that `v-for` requires. ## Migration Guide ```vue ``` ## Reference - [Vue.js Template Refs - Composition API](https://vuejs.org/guide/essentials/template-refs.html#accessing-the-refs) - [Vue 3.5 Release Notes](https://blog.vuejs.org/posts/vue-3-5)