0
0
Vueframework~20 mins

Hydration behavior in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Hydration Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens during Vue hydration when server and client markup differ?

Consider a Vue 3 app using SSR. The server renders a list of items, but the client has a different list initially. What will Vue do during hydration?

Vue
<template>
  <ul>
    <li v-for="item in items" :key="item.id">{{ item.text }}</li>
  </ul>
</template>

<script setup>
import { ref } from 'vue'
const items = ref([{ id: 1, text: 'A' }, { id: 2, text: 'B' }])
// Assume server rendered items: [{ id: 1, text: 'A' }, { id: 2, text: 'C' }]
</script>
AVue will keep the server markup as is and ignore the client data, causing the list to show 'C' instead of 'B'.
BVue will discard the server-rendered DOM and fully re-render the list on the client from scratch.
CVue will throw a hydration error and stop rendering because the server and client markup differ.
DVue will patch the DOM to update the text of the second list item from 'C' to 'B' without re-rendering the entire list.
Attempts:
2 left
💡 Hint

Think about how Vue tries to reuse server markup and patch only differences.

lifecycle
intermediate
2:00remaining
When does the mounted() hook run in a hydrated Vue component?

In a Vue 3 SSR app, the server renders the component and the client hydrates it. At what point does the mounted() lifecycle hook run?

AMounted runs immediately after the server renders the HTML, before hydration starts.
BMounted runs before hydration begins, during the initial client setup phase.
CMounted runs after the client finishes hydration and the component is attached to the DOM.
DMounted does not run at all during hydration to avoid duplicate side effects.
Attempts:
2 left
💡 Hint

Consider when the component is fully interactive on the client.

🔧 Debug
advanced
2:00remaining
Why does this Vue hydration warning appear in the console?

Given this Vue 3 SSR app, the console shows a hydration warning about mismatched content. What causes this warning?

Vue
<template>
  <div>{{ message }}</div>
</template>

<script setup>
import { ref } from 'vue'
const message = ref('Hello from client')
// Server rendered message was 'Hello from server'
</script>
AThe client data differs from the server markup, causing Vue to detect mismatch during hydration.
BThe component is missing a unique key, so Vue cannot match nodes properly.
CThe server did not render the component, so hydration fails.
DVue hydration warnings only appear if the component uses deprecated APIs.
Attempts:
2 left
💡 Hint

Check if the server and client render the same text content.

📝 Syntax
advanced
2:00remaining
Which Vue hydration code snippet correctly avoids hydration mismatch?

Choose the code snippet that prevents hydration mismatch by ensuring server and client render the same content.

A
&lt;template&gt;
  &lt;div&gt;{{ count }}&lt;/div&gt;
&lt;/template&gt;
&lt;script setup&gt;
import { ref, onMounted } from 'vue'
const count = ref(0)
onMounted(() =&gt; { count.value = 1 })
&lt;/script&gt;
B
&lt;template&gt;
  &lt;div&gt;{{ count }}&lt;/div&gt;
&lt;/template&gt;
&lt;script setup&gt;
import { ref } from 'vue'
const count = ref(1)
&lt;/script&gt;
C
&lt;template&gt;
  &lt;div v-if="mounted"&gt;{{ count }}&lt;/div&gt;
&lt;/template&gt;
&lt;script setup&gt;
import { ref, onMounted } from 'vue'
const count = ref(1)
const mounted = ref(false)
onMounted(() =&gt; { mounted.value = true })
&lt;/script&gt;
D
&lt;template&gt;
  &lt;div&gt;{{ count }}&lt;/div&gt;
&lt;/template&gt;
&lt;script setup&gt;
import { ref } from 'vue'
const count = ref(0)
&lt;/script&gt;
Attempts:
2 left
💡 Hint

Think about what value the server rendered and what the client starts with.

state_output
expert
2:00remaining
What is the final rendered output after hydration with this Vue 3 SSR component?

Given this Vue 3 SSR component, what will the user see after hydration completes?

Vue
<template>
  <button @click="increment">Clicked {{ count }} times</button>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(5) // server rendered with count=5
function increment() {
  count.value++
}
</script>
AThe button shows 'Clicked 5 times' initially, and increments count on each click.
BThe button shows 'Clicked 0 times' initially because client state resets, then increments on click.
CThe button shows 'Clicked 5 times' but clicking does not update the count due to hydration issues.
DThe button shows 'Clicked 0 times' and clicking causes hydration errors.
Attempts:
2 left
💡 Hint

Consider how Vue preserves reactive state from server to client during hydration.