Open code, practice, and notes →
Download source with English comments · for reading
Vue · 15 Template Refs and nextTick
Series contents · Technology map
Stage: Composables and Store Lifetimes · Editable example · Storage ID: vue-step-15
Goals and learning outcomes
- The textarea is the active element when the panel opens.
- There is no null access error while hidden.
Prerequisites
Unit 15 builds on the work and experiment records from Unit 14.
Key concepts and explanations
DOM references
A template ref points to the connected DOM element or component. It can be null when a conditional element is absent.
Batched updates
Vue batches DOM updates. After changing state, check after nextTick before handling actual DOM dimensions or focus.
Practice setup
First check the execution scope guidance on the example screen. The following is the preparation environment for extending the assignment in a separate project. Use .vue SFCs and script setup in a separate Vue Vite project. Record package versions and the lockfile, and run on the development server. The built-in Vue examples use the h function and are not a general-purpose SFC compiler.
The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.
Practice tasks
- Create a conditional textarea and ref in src/EditorPanel.vue.
- After setting visibility to true, connect await nextTick and focus.
- Repeat showing and hiding 5 times, checking the active element and null handling.
Variables to observe
| Variable | Baseline | Change | Expected change / hypothesis |
|---|---|---|---|
| Focus timing | Immediately after changing state | After nextTick | The created textarea can receive focus. |
Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.
Completion criteria
- The textarea is the active element when the panel opens.
- There is no null access error while hidden.
Self-check
- Does nextTick also wait for network completion?
- Can a computed result replace a DOM reference?
Official references
Linked example and execution scope
Example source · Runtime environments and limits
This is an experiment with actual Vue reactivity and render functions. Check SFC templates in an external compilation environment.
Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.
Next learning
Previous: Component Contracts and Watching · Next: Composable Lifetimes