0
0
Vueframework~20 mins

onUpdated and onBeforeUpdate in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Lifecycle Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Understanding onBeforeUpdate timing
In a Vue 3 component using the Composition API, when does the onBeforeUpdate lifecycle hook run?

Choose the correct description of its timing relative to the DOM update.
AIt runs right before the component's reactive data changes.
BIt runs after the DOM has been updated with the new data.
CIt runs only once when the component is first created.
DIt runs right before the DOM updates but after reactive data changes.
Attempts:
2 left
💡 Hint
Think about when Vue needs to prepare the DOM before showing changes.
component_behavior
intermediate
2:00remaining
Effect of onUpdated hook on DOM
What happens when the onUpdated lifecycle hook runs in a Vue 3 component?

Choose the correct statement about its effect on the DOM.
AIt runs after the DOM has updated and can access the new DOM state.
BIt runs before the DOM updates and can cancel the update.
CIt runs only if the component's props change, not reactive data.
DIt runs before the component is mounted.
Attempts:
2 left
💡 Hint
Consider when you want to read or manipulate the updated DOM.
state_output
advanced
2:00remaining
Output order of onBeforeUpdate and onUpdated
Given this Vue 3 component code, what is the order of console logs when the button is clicked?
import { ref, onBeforeUpdate, onUpdated } from 'vue';

export default {
  setup() {
    const count = ref(0);
    onBeforeUpdate(() => console.log('Before Update'));
    onUpdated(() => console.log('Updated'));
    function increment() {
      count.value++;
    }
    return { count, increment };
  }
}

When the button triggers increment(), what logs appear and in what order?
AUpdated, Before Update
BBefore Update, Updated
CBefore Update only
DUpdated only
Attempts:
2 left
💡 Hint
Remember the lifecycle hooks run around the DOM update.
🔧 Debug
advanced
2:00remaining
Why does onUpdated not run?
A developer writes this Vue 3 component:
import { ref, onUpdated } from 'vue';

export default {
  setup() {
    const message = ref('Hello');
    onUpdated(() => console.log('Component updated'));
    function changeMessage() {
      message.value = 'Hi';
    }
    return { message, changeMessage };
  }
}

But when changeMessage() is called, the console log never appears. What is the most likely reason?
AThe component is not mounted, so lifecycle hooks don't run.
BThe <code>onUpdated</code> hook only runs on prop changes, not reactive refs.
CThe component's template does not use <code>message</code>, so no DOM update occurs.
DThe <code>message</code> ref is not reactive, so no update triggers.
Attempts:
2 left
💡 Hint
Think about when Vue triggers DOM updates.
🧠 Conceptual
expert
2:00remaining
Distinguishing onBeforeUpdate and onUpdated use cases
Which scenario best fits using onBeforeUpdate instead of onUpdated in a Vue 3 component?
AYou want to save the current DOM state before it changes for comparison.
BYou want to fetch new data after the component updates.
CYou want to measure the DOM size after it changes to adjust layout.
DYou want to initialize data when the component mounts.
Attempts:
2 left
💡 Hint
Think about when you need to capture the old state before changes.