Chapter 47: Vue ‘ref’ Attribute

The ref attribute (written as ref=”someName” in the template)

This is not the same as the ref() function you use for reactive state (although both are called “ref” — yes, Vue naming is sometimes confusing on purpose).

Template ref = a way to get a direct reference (a handle) to a real DOM element (or to a Vue component instance) so you can talk to it imperatively in JavaScript.

In plain English:

“Hey Vue, please give me a way to find and control this particular <input>, <div>, <canvas>, or even <MyVideoPlayer> component later when I need to do something to it directly.”

You use template refs when Vue’s normal declarative reactivity (v-model, v-if, v-for, etc.) is not enough and you need imperative DOM access or component instance access.

Common Real-World Situations Where You Need Template Refs

  1. Focus an input field automatically or on button click
  2. Scroll to a specific message in a chat
  3. Measure the size/position of an element (for custom tooltips, popovers)
  4. Call methods on a third-party library widget (Chart.js, Leaflet map, Swiper carousel…)
  5. Play/pause a <video> or <audio> element
  6. Get canvas context (getContext(‘2d’))
  7. Call exposed methods on a child component
  8. Integrate legacy non-Vue JavaScript libraries

Modern Vue 3 Syntax (2026 – <script setup>)

There are two main patterns — both are correct, but one is cleaner and more common.

Pattern 1 – Single element / component (90% of cases)

vue

Pattern 2 – Multiple refs in a v-for loop

vue

Very Important Rules & Gotchas (2026 Edition)

Rule / Gotcha Correct Behavior / Solution
When is ref.value filled? After the component is mounted — before that it’s null
What if element is inside v-if? ref.value becomes null when condition is false — always check if (ref.value)
Multiple elements with same ref name? ref.value becomes an array automatically
v-for + ref Use callback syntax :ref=”(el) => messageRefs[index] = el” or just ref=”messageRefs”
Accessing child component instance ref.value is the Vue component instance — you can call exposed methods
Cleanup? Vue automatically sets ref.value = null on unmount — no manual cleanup needed
TypeScript typing `ref<HTMLInputElement
SSR / hydration? Refs are null on server — only populated client-side

Real-World Power Example – Accessing Child Component Instance

Child: VideoPlayer.vue (exposes controls)

vue

Parent – controls child imperatively

vue

Quick Cheat Sheet – When to Use Template Refs

Goal Typical Code Pattern When ref.value is safe?
Auto-focus input on mount ref=”input” + onMounted(() => input.value.focus()) After onMounted
Scroll to specific message ref=”lastMsg” + lastMsg.value.scrollIntoView() After mount / nextTick
Get element dimensions const rect = el.value.getBoundingClientRect() After mount
Control 3rd-party widget ref=”chart” + chart.value?.chartInstance.update() After mount
Call exposed method on child component defineExpose({ play }) + child.value.play() After mount
Multiple refs in loop ref=”items” → items.value[index] After mount

Pro Tips from Real Projects (Hyderabad 2026)

  • Always check if (ref.value) — refs are null before mount and after unmount
  • Use nextTick() when you change state and immediately need updated DOM
  • Prefer declarative solutions (v-model, v-if, transitions) → use refs only when you must go imperative
  • For third-party libraries (Chart.js, Mapbox, Swiper, Video.js…) → template refs are usually the main integration point
  • In TypeScript → type your refs → avoids “Property ‘focus’ does not exist on type ‘Ref<null>'” errors

Your mini practice task:

  1. Create an input that auto-focuses on mount (template ref + onMounted)
  2. Add a long scrollable list with many items
  3. Add a “Jump to bottom” button using a ref on the last item
  4. Add a button that focuses the input again

Any part confusing? Want full examples for:

  • Template refs + <Transition> for animated focus?
  • Accessing child component methods with TypeScript?
  • Common bug with v-if + ref?
  • Real Chart.js / Google Maps integration using refs?

Just tell me — we’ll build the next practical ref example together 🚀

Happy reffing from Hyderabad! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *