So, I’ve been playing around with Svelte, this unique JavaScript compiler, for a while now, and let me tell you, it’s awesome! As someone who’s experienced with React, using Svelte feels like a refreshing change of pace. I’m not here to pit React against Svelte - plenty of articles already do that. Instead, I want to share some cool stuff I’ve learned while using Svelte.
Contents
- Svelte Spring
- Svelte Transitions
- Keyed Each Blocks
- Awaited Blocks
- Sending Messages Between Components
- Named Slots
- Named Exports
@const
and@html
Tags- Conclusion
Svelte Spring
Svelte comes with a built-in spring animation library that’s super easy to use and lightning-fast! You just specify the spring parameters and boom! The animation takes care of itself. Check out this example:
And voila! You get a nice springy animation! It’s that simple.
Learn more: Svelte Tutorial - Tweened, Svelte Tutorial - Spring
Svelte Transitions
But wait, there’s more! Svelte not only has spring animations, but it also comes with a transition library. Adding a transition is a piece of cake - just slap the transition
attribute on an element.
You’ve got a sliding effect like magic!
Svelte also has something called Deferred Transitions, which only run when an element is added or removed from the DOM. Check out this icon morphing to a div example:
The best part? It’s less than 90 lines of code!
Learn more: Svelte Tutorial - Transition, Svelte Deferred Transitions
Keyed Each Blocks
When it comes to lists in Svelte, you use the each
block. Svelte handles the key for you by default, but if you want more control, you can specify a key yourself.
Just add (item.id)
after the each
block to specify the key.
Awaited Blocks
Here’s a nifty feature: using awaited
blocks when you want to wait for a promise to resolve before rendering something.
Take this example using the fetch
API to get a random number from a server:
Sending Messages Between Components
Picture this: you’ve got a component rendering a list of items, and each item has a button that deletes it. You want to update the list when an item is deleted, but how do you do that?
With Svelte’s createEventDispatcher
function, you can send a message from the child component to the parent component.
In the child component, create a dispatcher and dispatch a message when the button is clicked:
In the parent component, listen for the message and update the list:
Named Slots
In Svelte, when you’re working with slots, you have the option to name them, which comes in handy when you want to pass multiple slots to a component.
Let’s say we have a card component like this:
We can use our card component like this:
By adding a slot
attribute to the element we want to pass to the slot and setting the value to the name of the slot, we can achieve this. It allows us to customize different parts of the card easily.
Named Exports
By default, Svelte exports a component as the default export. However, if you need additional exports, you can use named exports.
In your component file:
By adding the context="module"
attribute to the first script tag, Svelte recognizes it as a module script.
You can then import the named export like this:
This allows you to have multiple exports from a single component.
@const
and @html
Tags
While less known, the @const
and @html
tags can be useful in certain situations.
@const
Inside an {#if}
or {#each}
block, you can use the @const
tag to create a constant variable within the block.
This saves time by avoiding repetitive code and duplications.
@html
The @html
tag allows you to render HTML inside a component. Unlike React’s dangerouslySetInnerHTML
, Svelte’s @html
is straightforward to use. However, be cautious when using it, as it can lead to XSS attacks.
It provides a way to insert dynamic HTML content into your component, but be sure to sanitize any user-generated content before using it to prevent security issues.
Conclusion
In conclusion, Svelte has proven to be a fantastic JavaScript compiler with its intuitive features like spring animations, transitions, named slots, and more. It offers a refreshing alternative to other frameworks. So, give it a try and start building amazing web applications with Svelte today!