Dynamic Component Rendering with <code>svelte:component</code>
📖 Scenario: You are building a simple Svelte app that shows different greeting messages depending on the user's mood. Each mood has its own small component that displays a unique message.
🎯 Goal: Build a Svelte app that uses svelte:component to dynamically render one of three greeting components based on a selected mood.
📋 What You'll Learn
Create three Svelte components named
Happy.svelte, Sad.svelte, and Excited.svelte with distinct greeting messages.Create a main
App.svelte file that holds the current mood in a variable called currentMood.Use a configuration variable
components that maps mood names to their respective components.Use
svelte:component in App.svelte to render the component matching currentMood.Add buttons to change
currentMood dynamically.💡 Why This Matters
🌍 Real World
Dynamic component rendering is useful in apps where the UI changes based on user choices or data, such as dashboards, forms, or multi-step wizards.
💼 Career
Understanding <code>svelte:component</code> helps you build flexible and maintainable Svelte apps that adapt to user input or external data, a common requirement in frontend development jobs.
Progress0 / 4 steps