Why lifecycle hooks run code at key moments
📖 Scenario: You are building a simple Svelte component that shows a message when it appears on the screen and cleans up when it disappears. This helps understand how lifecycle hooks let you run code at important times.
🎯 Goal: Create a Svelte component that uses lifecycle hooks onMount and onDestroy to show messages in the console when the component loads and unloads.
📋 What You'll Learn
Create a Svelte component with a message variable
Use
onMount lifecycle hook to set the message to 'Component is mounted!'Use
onDestroy lifecycle hook to set the message to 'Component is destroyed!'Display the message in the component's HTML
💡 Why This Matters
🌍 Real World
Lifecycle hooks help run code at important times like loading data when a page opens or cleaning up timers when a page closes.
💼 Career
Understanding lifecycle hooks is key for building interactive web apps that respond correctly to user actions and system events.
Progress0 / 4 steps