0
0
Vueframework~20 mins

Template refs for DOM access in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Ref Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when accessing a template ref in Vue 3 Composition API?
Consider this Vue 3 component using ref to access a DOM element. What will be logged when the button is clicked?
Vue
<template>
  <input ref="inputRef" />
  <button @click="logInputValue">Log Value</button>
</template>

<script setup>
import { ref } from 'vue'
const inputRef = ref(null)

function logInputValue() {
  console.log(inputRef.value.value)
}
</script>
ALogs the current text inside the input box.
BLogs undefined because inputRef.value is null.
CThrows a runtime error because inputRef is not defined.
DLogs the DOM element itself, not its value.
Attempts:
2 left
💡 Hint
Remember that inputRef.value points to the DOM element, and .value on that element is the input's text.
📝 Syntax
intermediate
2:00remaining
Which option correctly defines and uses a template ref in Vue 3 <script setup>?
Select the code snippet that correctly declares a template ref and accesses the DOM element's text content.
A
&lt;template&gt;
  &lt;textarea ref="textAreaRef"&gt;&lt;/textarea&gt;
&lt;/template&gt;
&lt;script setup&gt;
import { ref } from 'vue'
const textAreaRef = ref()
function showText() {
  alert(textAreaRef.value.value)
}
&lt;/script&gt;
B
&lt;template&gt;
  &lt;textarea ref="textAreaRef"&gt;&lt;/textarea&gt;
&lt;/template&gt;
&lt;script setup&gt;
const textAreaRef = null
function showText() {
  alert(textAreaRef.value.value)
}
&lt;/script&gt;
C
&lt;template&gt;
  &lt;textarea ref="textAreaRef"&gt;&lt;/textarea&gt;
&lt;/template&gt;
&lt;script setup&gt;
import { ref } from 'vue'
const textAreaRef = ref(null)
function showText() {
  alert(textAreaRef.value.value)
}
&lt;/script&gt;
D
&lt;template&gt;
  &lt;textarea ref="textAreaRef"&gt;&lt;/textarea&gt;
&lt;/template&gt;
&lt;script setup&gt;
import { reactive } from 'vue'
const textAreaRef = reactive(null)
function showText() {
  alert(textAreaRef.value.value)
}
&lt;/script&gt;
Attempts:
2 left
💡 Hint
Template refs need to be initialized with ref(null) to work properly.
🔧 Debug
advanced
2:00remaining
Why does this Vue 3 code throw an error when accessing a template ref?
Given this code, what is the cause of the error when clicking the button?
Vue
<template>
  <input ref="myInput" />
  <button @click="focusInput">Focus</button>
</template>

<script setup>
import { ref } from 'vue'
const myInput = ref(null)

function focusInput() {
  myInput.focus()
}
</script>
AThe input element does not support the <code>focus</code> method.
BThe error happens because <code>ref</code> was not imported from Vue.
CThe button click event is not properly bound, so the function is never called.
DThe code throws an error because <code>myInput</code> is a ref object and needs <code>.value</code> to access the DOM element.
Attempts:
2 left
💡 Hint
Remember that refs are objects with a value property holding the actual DOM element.
state_output
advanced
2:00remaining
What is the value of inputRef.value.value after mounting this Vue 3 component?
Analyze the component below. What will console.log(inputRef.value.value) output right after the component is mounted?
Vue
<template>
  <input ref="inputRef" value="Hello Vue" />
</template>

<script setup>
import { ref, onMounted } from 'vue'
const inputRef = ref(null)
onMounted(() => {
  console.log(inputRef.value.value)
})
</script>
Anull
B"Hello Vue"
Cundefined
DThrows a runtime error because inputRef is null
Attempts:
2 left
💡 Hint
The value attribute sets the initial text inside the input element.
🧠 Conceptual
expert
3:00remaining
Why should you avoid using template refs for reactive data in Vue 3?
Which statement best explains why template refs should not be used as the main way to manage reactive data in Vue 3?
ATemplate refs point to DOM elements and do not trigger reactivity updates when their properties change.
BTemplate refs automatically update reactive data, so using them causes infinite loops.
CTemplate refs are deprecated in Vue 3 and should not be used at all.
DTemplate refs can only be used inside <code>&lt;script&gt;</code> tags, not <code>&lt;script setup&gt;</code>.
Attempts:
2 left
💡 Hint
Think about how Vue tracks changes and updates the UI.