Svelte a new contender to the war of JavaScript frameworks. It might not be as mature as other frameworks like React and Vue, but here are three reasons you should try Svelte:
- The learning curve is pretty small
- It requires less amount of lines & has easy state management.
- It is not a normal framework.
1 . Learning curve is pretty small
If you know HTML ,CSS & Javascript, learning Svelte is going to be a breeze. Like Vue, Svelte templates are a superset of HTML.
In first glance everything looks like HTML but it is Svelte. The {} are usually used to insert Javascript in HTML.
Svelte is HTML ,CSS ,Javascript with many cool add-ons.
If you know only HTML ,CSS ,Javascript than give Svelte a try. Later transitioning to other frameworks will be easy too.
2 . It requires less amount of lines & has easy state management.
Let's take an example of a simple counter with React & Svelte
React :
class Timer extends React.Component {
constructor(props) {
super(props);
this.state = { seconds: 0 };
}
tick() {
this.setState(state => ({
seconds: state.seconds + 1
}));
}
componentDidMount() {
this.interval = setInterval(() => this.tick(), 1000);
}
componentWillUnmount() {
clearInterval(this.interval);
}
render() {
return (
<div>
Seconds: {this.state.seconds}
</div>
);
}
}
ReactDOM.render(
<Timer />,
document.getElementById('timer-example')
);
Svelte :
<script>
let seconds = 0;
setInterval(() => seconds += 1, 1000);
</script>
Seconds: {seconds}
React: 33 lines ,
Svelte: 6 lines
As seen from above :
- Svelte does greater things in few lines of code.
- Svelte 's state management is really simple.
3 . It isn't a normal framework..
Svelte is pretty different from other frameworks. Svelte compiles you code into vanilla javascript. Thus the build size of Svelte apps is small. Svelte deserves its reputation because of its speed and developer experience.
Top comments (24)
react with hook is 9 line, but this component can be instanced any amount, and after remove closeInterval properly. Maybe seems complex, but you can use modern vanilla JS / JSX.
implement example
each timer have own state
React is still terse. But what is
closeInterval
? Did you meanclearInterval
? And I prefer using custom hooks to encapsulate ”reference fragile” behavior. For example by makinguseInterval
hook or utilize the one from rooks packageyou right, clearInterval of course!
I used React for the first few years right after it was announced and absolutely loved it. For the past year and a half, I've used Svelte exclusively and wouldn't go back. Here's a lot more detail on why: mikenikles.com/blog/why-i-moved-fr...
As for your example with hooks: In my opinion, this is near impossible to understand without a deep knowledge of React which likely takes many hours of reading and using to acquire.
The Svelte example on the other hand is self-explanatory. With an hour of effort to work through the interactive Svelte tutorial, most people are ready to be productive.
If team efficiency, developer experience, time to market, maintainability and end user performance experience are important measures, I'd highly recommend Svelte over React in most cases.
It's important to note, and the author mentioned that too, that Svelte is less established. Fewer jobs, fewer opportunities to use it on a day job etc. However, that's the case for any new framework. Svelte is adopted quickly among big companies such as Apple and Spotify.
Good points, thanks!
But #3 contains a common mistake when describing svelte – its difference is that it compiles to imperative JS, not vanilla. All top frameworks compile to vanilla JS
Yes, agree with the headline of this article, you should try Svelte and not just because is the new "cool kid on the block".
For me, it was a breath of fresh air to be able to make websites with as little extra things possible and still have several functionalities baked into the app.
I personally like how you can have everything necessary for a component in a single file and not needing to go back and forth between styles, markup and js.
I have already used it 2 times to make business type websites for my father and a good friend of mine and it almost feels like I was just using plan HTML + CSS plus some cool functionalities you can implement with just few lines of JS.
I'm not a big React fanboy but, I'm not sure about Svelte. Blending styles, controllers and presentation into one file feels wrong. Also, I don't know if in Svelte it's possible (or how hard it would be) to implement an app state management with central store(s). I think the comparison would be only fair in a more advanced use case.
Svelte allows you to declare store in a file, and
import
them into components. If the value of a store changes, all components importing this store are notified and updated.cool, that's what I would expect :)
svelte.dev/tutorial/writable-stores
Most of my career I've been using React, but recently I decided to try Svelte for a project. I think I still prefer React approach to building UI. But that's my personal opinion. But one thug bothers me in that article, why are co comparing Svelte to React example that uses classes? It's quite 'old' approach and you can do everything with hooks much easier and cleaner. I just think these kind of comparisons don't give a real justice.
I tried react and vue and dont like it.
I think that Svelte and Nim are better giving you more features without the need of linking other additional libraries.
You should try Nim if you liked Svelte.
😃
Or you could try RiotJS which appears to have heavily influenced Svelte, and is a lot more mature - it's on version 4 (version 5 is in alpha)and has been around since 2013. A similar timer component would be defined as follows:
I only know base of HTML, CSS, and JS (plus, little knowledge about Vue). Then, I try React but it's difficult for me. I try Svelte, then, I can create website with it. It's so easy! 🥳🥳🥳
I build my blog with Svelte.
imo it is just even easier to write functional react components for stuff like that so you may want to consider that when comparing line count
Also, the div is missing in the svelte version, anche the timeout Is never cleared. So it's not the same example.
The word vanilla is confusing here IMO