0
0
Svelteframework~10 mins

onMount 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 onMount function from Svelte.

Svelte
import { [1] } from 'svelte';
Drag options to blanks, or click blank then click option'
AonMount
BuseEffect
CcomponentDidMount
DonInit
Attempts:
3 left
💡 Hint
Common Mistakes
Using React lifecycle functions like useEffect or componentDidMount instead of onMount.
Trying to import a function that doesn't exist in Svelte.
2fill in blank
medium

Complete the code to run a function when the component mounts.

Svelte
onMount(() => { [1] });
Drag options to blanks, or click blank then click option'
AsetTimeout()
Breturn () => {}
Cconsole.log('Mounted!')
Dexport default
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to export inside the onMount callback.
Using setTimeout without a function.
3fill in blank
hard

Fix the error in the onMount usage by completing the code.

Svelte
onMount([1] () => {
  console.log('Ready');
});
Drag options to blanks, or click blank then click option'
Aasync
Bawait
Cfunction
Dexport
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'await' before the function keyword.
Trying to export inside the callback.
4fill in blank
hard

Fill both blanks to correctly import and use onMount with a cleanup function.

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

onMount(() => {
  const id = setInterval(() => console.log('Tick'), 1000);
  return () => clearInterval([2]);
});
Drag options to blanks, or click blank then click option'
AonMount
Bid
Cinterval
DsetInterval
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable name in clearInterval.
Importing a wrong function name.
5fill in blank
hard

Fill all three blanks to create a reactive variable updated on mount.

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

let count = 0;

onMount(() => {
  const timer = setInterval(() => {
    count [2] count + 1;
  }, 1000);
  return () => clearInterval([3]);
});
Drag options to blanks, or click blank then click option'
AonMount
B=
Ctimer
D+=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+=' instead of '=' for updating count (works but not the intended answer).
Clearing interval with wrong variable.