Lifecycle methods help you run code at specific times in a component's life. In nested components, they let each part do its own setup and cleanup.
Lifecycle in nested components in Svelte
import { onMount, beforeUpdate, afterUpdate, onDestroy } from 'svelte'; onMount(() => { // code to run when component mounts }); beforeUpdate(() => { // code before component updates }); afterUpdate(() => { // code after component updates }); onDestroy(() => { // code when component is removed });
Each lifecycle function runs only inside the component where it is declared.
Nested components have their own lifecycle independent from their parents.
import { onMount } from 'svelte'; onMount(() => { console.log('Child component mounted'); });
import { onDestroy } from 'svelte'; onDestroy(() => { console.log('Child component removed'); });
import { beforeUpdate, afterUpdate } from 'svelte'; beforeUpdate(() => { console.log('Child will update soon'); }); afterUpdate(() => { console.log('Child updated'); });
This example shows a parent component that includes a child component. Both have onMount and onDestroy lifecycle functions that log messages. When the parent and child appear or disappear, their own lifecycle code runs separately.
Parent.svelte: <script> import Child from './Child.svelte'; import { onMount, onDestroy } from 'svelte'; onMount(() => { console.log('Parent mounted'); }); onDestroy(() => { console.log('Parent destroyed'); }); </script> <Child /> Child.svelte: <script> import { onMount, onDestroy } from 'svelte'; onMount(() => { console.log('Child mounted'); }); onDestroy(() => { console.log('Child destroyed'); }); </script> <p>This is the child component content.</p>
Lifecycle functions in nested components run independently and in order of mounting.
When a parent is removed, its children are removed too, triggering their onDestroy functions.
Use console logs or DevTools to see lifecycle events in action.
Each nested component has its own lifecycle methods.
Use onMount and onDestroy to run setup and cleanup code.
Lifecycle methods help manage resources and actions in nested parts of your app.
Practice
Solution
Step 1: Understand lifecycle order in nested components
When a child component is added to the DOM, itsonMountruns after the component is rendered.Step 2: Identify the first lifecycle method after mounting
onMountis the first lifecycle method called after the component is inserted, before any updates or destruction.Final Answer:
onMount -> Option AQuick Check:
Child component mounts first with onMount [OK]
- Confusing onDestroy as first method
- Thinking beforeUpdate runs before onMount
- Assuming afterUpdate runs on initial mount
onMount in a nested Svelte component?Solution
Step 1: Check correct import syntax
The correct import isimport { onMount } from 'svelte';using named import from 'svelte'.Step 2: Verify usage of onMount function
CallingonMount(() => { ... })registers the callback properly. Assigning onMount to a function is incorrect.Final Answer:
import { onMount } from 'svelte'; onMount(() => { console.log('Mounted'); }); -> Option CQuick Check:
Named import and callback usage [OK]
- Using default import instead of named import
- Importing from wrong path like 'svelte/lifecycle'
- Assigning onMount instead of calling it
onMount hooks that log messages. What is the order of console logs when the parent is rendered?Solution
Step 1: Understand mounting order in nested components
Child components mount before their parents finish mounting, so child'sonMountruns first.Step 2: Confirm console log order
Child'sonMountlogs first, then parent'sonMountlogs after child is fully mounted.Final Answer:
Child's onMount logs first, then parent's onMount -> Option AQuick Check:
Child mounts before parent finishes [OK]
- Assuming parent mounts before child
- Thinking onMount runs simultaneously
- Believing onMount doesn't run in nested components
onDestroy hook, but it never runs when the component is removed. What is the most likely cause?Solution
Step 1: Check if component is removed from DOM
onDestroyonly runs when the component is removed. If it stays in DOM, it won't run.Step 2: Verify other causes
ImportingonDestroyincorrectly or mixing hooks causes errors, but the main reason is component not unmounting.Final Answer:
The component is never actually removed from the DOM -> Option BQuick Check:
onDestroy runs only on removal [OK]
- Expecting onDestroy without unmounting
- Confusing onMount with onDestroy
- Assuming parent's lifecycle affects child's onDestroy
onMount and cleans up a subscription in onDestroy. If the parent conditionally renders this child multiple times, what is the best practice to avoid memory leaks?Solution
Step 1: Understand lifecycle with conditional rendering
When the child mounts and unmounts repeatedly,onDestroymust clean up resources each time to prevent leaks.Step 2: Evaluate best practice for cleanup
Properly unsubscribing inonDestroyensures no leftover subscriptions remain after unmount.Final Answer:
Ensure the child's onDestroy properly unsubscribes every time it unmounts -> Option DQuick Check:
Clean up in onDestroy to avoid leaks [OK]
- Ignoring cleanup in onDestroy
- Moving logic unnecessarily to parent
- Using beforeUpdate for cleanup instead of onDestroy
Ready to prove it?
Pass the test and this pattern moves unknown
