Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

svelte
dom
datatrack
communitycreator

What is beforeUpdate() and afterUpdate() in Svelte?

Saad Akbar

DOM is a browser state that keeps track of the data in the application. beforeUpdate is a function that executes just before the state of the DOM is updated. afterUpdate executes immediately after the DOM is synchronized with the code’s data. In other words, it executes when the DOM is updated. This implementation is useful in case you want to track any of the actions on the DOM or application.

Code

Suppose you want to track when to auto-scroll on a chatbox in a web application. These functions can be used to implement that feature.

<script>
	import { beforeUpdate, afterUpdate } from 'svelte';

	beforeUpdate(() => {
		// determine whether we should auto-scroll
		// once the DOM is updated...
	});

	afterUpdate(() => {
		// ...the DOM is now in sync with the data
	});
</script>

RELATED TAGS

svelte
dom
datatrack
communitycreator
RELATED COURSES

View all Courses

Keep Exploring