Vue: TypeError: Cannot read properties of undefined (reading 'focus') — template ref accessed before the component is mounted
A template ref (ref="input") is read before the element exists (setup, created, or before v-if renders it).
TypeError: Cannot read properties of undefined (reading 'focus') template refCannot read properties of null (reading 'focus') ref valuethis.$refs.input is undefined Vue[Vue warn]: Unhandled error during execution of mounted hook TypeError: Cannot read properties of null (reading 'focus')Vue template ref null before mount Meaning
Template refs are populated after mount. Use them in onMounted/nextTick, check for null (inputRef.value?.focus()), and remember refs inside v-if/v-for appear later. In <script setup> declare const input = ref(null) with the same name as the ref attribute; with v-for refs are arrays.
Common causes
- Accessing refs in
setup()/created - Name mismatch between
refattribute and variable - Element under
v-ifnot rendered yet v-forrefs are arrays- Child component refs undefined until mounted
⚡ Quick fix
- Use
onMounted(() => input.value?.focus()) - Match
const input = ref(null)toref="input" await nextTick()after togglingv-if
Detailed fix by platform
Vue
<input ref="input"><script setup>const input = ref(null); onMounted(() => input.value?.focus());</script>
How to diagnose
- Timing — After mount?
- Name — Matches?
- v-if — Rendered?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- [Vue warn]: onMounted is called when there is no active component instance Vue: [Vue warn]: onMounted is called when there is no active component instance to be associated with
- [Vue warn]: Unhandled error during execution of setup/mounted hook Vue: [Vue warn]: Unhandled error during execution of mounted hook / TypeError: Cannot read properties of undefined (reading 'x')
- Duplicate attribute (Vue template) Vue: [plugin:vite:vue] Duplicate attribute. / [vue/no-duplicate-attributes] Duplicate attribute 'class'
Most viewed in Vue
- Failed to resolve component Vue: [Vue warn]: Failed to resolve component: x
- Avoid mutating a prop directly Vue: [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders
- Nuxt: window/document/localStorage is not defined (SSR) Nuxt: ReferenceError: window is not defined / 500 Internal Server Error during SSR
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 5 Nov 2026