A. onMount should be used inside <script> tag only
B. Missing import statement
C. Callback function is missing parentheses
D. onMount is assigned instead of called
Solution
Step 1: Check how onMount is used
The code assigns a function to onMount instead of calling onMount() with a callback.
Step 2: Correct usage of onMount
It should be onMount(() => { console.log('Mounted'); }); to register the callback properly.
Final Answer:
onMount is assigned instead of called -> Option D
Quick Check:
Assigning onMount instead of calling it = A [OK]
Hint: Call onMount with a function, don't assign it [OK]
Common Mistakes:
Assigning onMount instead of calling
Forgetting parentheses on callback
Confusing import errors with usage errors
5. You want to fetch data from an API only once when a Svelte component appears, and clean up a timer when it disappears. Which is the best way to do this using onMount?
hard
A. Use onMount to fetch data and return a cleanup function to clear the timer
B. Fetch data outside onMount and start timer inside onMount without cleanup
C. Use onMount to fetch data and start timer but do not return cleanup
D. Start timer outside onMount and fetch data inside onMount without cleanup
Solution
Step 1: Understand onMount usage for setup
onMount runs once after component appears, perfect for fetching data and starting timers.
Step 2: Cleanup function must be returned from onMount
To clear timers on unmount, return a cleanup function inside onMount. Use onMount to fetch data and start timer but do not return cleanup misses cleanup, so it's incomplete.
Step 3: Evaluate options
Use onMount to fetch data and return a cleanup function to clear the timer correctly fetches data and returns cleanup function to clear timer, which is best practice.
Final Answer:
Use onMount to fetch data and return a cleanup function to clear the timer -> Option A
Quick Check:
Fetch + cleanup in onMount = D [OK]
Hint: Return cleanup function inside onMount for timers [OK]
Common Mistakes:
Not returning cleanup function to clear timers
Fetching data outside onMount causing multiple calls
Starting timers without cleanup causing leaks
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.