Vue 'updated' Lifecycle Hook
Example
Using the updated
lifecycle hook to write a message to the console every
time the DOM tree is updated.
export default {
data() {
return {
sliderVal: 50,
renderCount: 0
}
},
updated() {
this.renderCount++;
console.log('Updated ' + this.renderCount + ' times.')
}
}
Run
Example »
Definition and Usage
The updated
lifecycle hook happens right after the DOM tree has updated.
If we modify a property or do something else in the updated
hook that
triggers a new render, the updated
hook will be called again after that new
render, and we have most likely created an infinite loop.
To avoid an infinite loop we should always consider to use the beforeUpdate
lifecycle hook instead of the updated
lifecycle hook.
The this.$nextTick()
or nextTick()
functions can also be used to run code after the DOM has been updated.
Note: In the example above, we write the render count to the console, because making
changes to the view would re-activate the updated
hook and create an
infinite loop.
Related Pages
Vue Tutorial: Vue Lifecycle Hooks
Vue Tutorial: The 'beforeUpdate' Hook
Vue Reference: Vue 'beforeUpdate' Lifecycle Hook
Vue Reference: Vue $nextTick() Method