0
0
Svelteframework~10 mins

beforeUpdate and afterUpdate in Svelte - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the lifecycle function that runs before the DOM updates.

Svelte
import { [1] } from 'svelte';
Drag options to blanks, or click blank then click option'
AbeforeUpdate
BafterUpdate
ConMount
DonDestroy
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'afterUpdate' which runs after the DOM updates.
Confusing with 'onMount' which runs once when component mounts.
2fill in blank
medium

Complete the code to run a function after the DOM updates.

Svelte
afterUpdate(() => { [1] });
Drag options to blanks, or click blank then click option'
Aconsole.log('On mount')
Bconsole.log('Before update')
Cconsole.log('After update')
Dconsole.log('On destroy')
Attempts:
3 left
💡 Hint
Common Mistakes
Logging 'Before update' inside afterUpdate.
Using onMount or onDestroy messages here.
3fill in blank
hard

Fix the error in the code to correctly import both lifecycle functions.

Svelte
import { [1] } from 'svelte';
Drag options to blanks, or click blank then click option'
AbeforeUpdate, afterUpdate
BafterUpdate
CbeforeUpdate
DonMount
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing only one function when both are needed.
Using invalid or unrelated function names.
4fill in blank
hard

Fill both blanks to run code before and after the DOM updates.

Svelte
import { [1], [2] } from 'svelte';

[1](() => console.log('Before update'));
[2](() => console.log('After update'));
Drag options to blanks, or click blank then click option'
AbeforeUpdate
BonMount
CafterUpdate
DonDestroy
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up beforeUpdate and afterUpdate.
Using onMount or onDestroy instead.
5fill in blank
hard

Fill all three blanks to create a Svelte component that logs messages before update, after update, and on mount.

Svelte
<script>
import { [1], [2], [3] } from 'svelte';

[1](() => console.log('Before update'));
[2](() => console.log('After update'));
[3](() => console.log('On mount'));
</script>
Drag options to blanks, or click blank then click option'
AbeforeUpdate
BafterUpdate
ConMount
DonDestroy
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing the order of lifecycle functions.
Using onDestroy instead of onMount.