Discover how a single tag can make your app switch views like magic!
Why svelte:component for dynamic components? - Purpose & Use Cases
Imagine you want to show different parts of your app based on user choices, like switching between a login form, a profile view, or a settings page.
Doing this by hand means writing lots of if-else blocks and manually adding or removing components in your code.
Manually swapping components is messy and hard to maintain.
You have to write repetitive code and it's easy to make mistakes, like forgetting to update something or causing flickers on the screen.
The svelte:component tag lets you pick which component to show dynamically with a simple variable.
This means you write cleaner code and Svelte handles the switching smoothly for you.
if (page === 'login') { show LoginComponent } else if (page === 'profile') { show ProfileComponent }
<svelte:component this={currentComponent} />You can build flexible apps that change views instantly based on user actions, without messy code.
Think of a dashboard app where clicking menu items instantly swaps the main content area to different tools or reports.
Manually switching components is repetitive and error-prone.
svelte:component lets you swap components easily using a variable.
This makes your code cleaner and your app more responsive.