---
title: Template Refs Become Null When Elements Are Unmounted
impact: MEDIUM
impactDescription: Refs become null when conditionally rendered elements are removed, causing errors if not handled
type: gotcha
tags: [vue3, template-refs, v-if, watchers, conditional-rendering]
---
# Template Refs Become Null When Elements Are Unmounted
**Impact: MEDIUM** - When using template refs with `v-if`, the ref becomes `null` when the element is unmounted. Watchers and effects that access these refs must handle the null case to avoid runtime errors.
This is especially tricky with `watchEffect` since it runs automatically and may execute when the ref is null.
## Task Checklist
- [ ] Always check for null before accessing ref properties when using v-if
- [ ] In watchers, explicitly handle the null case (element unmounted or not yet mounted)
- [ ] Consider whether v-show is more appropriate if you need persistent ref access
- [ ] Use optional chaining (?.) when accessing ref properties in uncertain contexts
**Incorrect:**
```vue
```
**Correct:**
```vue
```
```vue
```
```vue
```
```vue
```
## Reference
- [Vue.js Template Refs](https://vuejs.org/guide/essentials/template-refs.html)