React Hooks vs Svelte - Why I chose Svelte?
Tags: react, svelte, reacthooks, reactvssvelte
Published: 2021-08-12 12:59:29

I work at ZoomRx and I have been developing web applications for the past 10 years. ZoomRx is a strategic healthcare consulting firm. All of my front-end colleagues are strong in Javascript fundamentals, so we have the leverage to adopt and experiment with new frameworks frequently. I experienced a lot of javascript libraries/frameworks, especially Jquery, Ember, Mithril, Angular, React and React with Hooks. I love a few aspects in each of these frameworks but I'm not fulfilled and never stopped looking for alternatives. I was just looking for a framework which is developer friendly and highly performant. I even had ideas for building my own framework. Then In the middle of 2019 I came across Svelte and was impressed by [Rich Harris’s speech](https://youtu.be/AdNJ3fydeao) (It's worth a try even though it is an old one now). Svelte does the reactivity through the compiler and all of the heavy lifting has been done by the compiler.

React Hooks vs Svelte, There is a lot more to discuss about it but for this article I’ll give you my perspective about two important aspects of the frameworks, better developer experience and better user experience.

### Better developer experience
* **Learning curve**

Since Svelte tries to follow the web standard as much as possible, learning Svelte is very easy. As a javascript developer, it took just [5 minutes to understand](https://youtu.be/rv3Yq-B8qp4) to get the basic idea, and a few hours to master all concepts. I’m that curious guy who always jumps into the underlying framework code before using it. It makes me more comfortable while coding. So I forked the svelte compiler code and used TanLiHau’s [compiler](https://lihautan.com/the-svelte-compiler-handbook/) [handbook](https://lihautan.com/compile-svelte-in-your-head-part-2/) to understand the compiler architecture. The whole process took a weekend to make myself comfortable. At the end of two days, I did some POCs like counter, todo lists, built a trello like app and a wrapper for [flatpickr](https://flatpickr.js.org/).

Although I knew the React class component well, it almost took a week to understand all the nuances of hooks like the why, where and how to use it effectively. This [blog](https://the-guild.dev/blog/react-hooks-system) will help you to [learn](https://eliav2.github.io/how-react-hooks-work/) more about the underlying hooks system.

Apparently the Svelte learning curve was much smaller than React Hooks for me. With Svelte, any Javascript developer can learn the basic principles of a component state without getting confused by the details too much. Also [Svelte store](https://svelte.dev/tutorial/writable-stores) is simpler to learn and use when compared to Redux/ React’s context API/useContext /Zustand or Jotai etc.,

* **Readability and Maintenance**

We have been using a lot of frameworks in our products. So readability and clarity are more important to me than any new fancy features.

> “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” ― Martin Fowler

*[React Hook](https://codesandbox.io/s/react-hook-hell-g6kos?file=/src/App.js:0-1700) - sample code to capture mouse coordinates*
{% gist https://gist.github.com/dhinesh03/a32ab043634ff15c33d17de890563e93 %}

*[Svelte](https://svelte.dev/tutorial/inline-handlers) - sample code to capture mouse coordinates*
{% gist https://gist.github.com/dhinesh03/e5a43efa300829bf6879cff2b394e94c %}

>“Let us forget about the number of lines of code that we needed to write. Assume you just know Javascript well, you are reading how to capture mouse coordinates during movement. Which code looks more like what you actually need?”

As seen from above, Svelte does great things in a few lines of code. It's easier to read and understand by anyone who has some basic HTML/CSS/JS knowledge. It completely removes all of the unnecessary boilerplates like useState, useRef, useCallback, useMemo etc., from our application logic. Writing less code will help us with less maintenance and more time to develop new features. Read more about the readability part on [Rich Harris’s blog](https://svelte.dev/blog/write-less-code). On a side note:  If the same example was written using React class component, it would be way more readable than the one using Hooks.

* **External library integration**

React uses Virtual DOM so we need a special wrapper to handle the state of the external library components. But, the React community is extremely huge so you can find wrappers or equivalent React implementation easily. On the other hand, Svelte deals with the real DOM. This makes things simple and easy when doing some CSS Animations and DOM manipulations. You can use any DOM libraries directly or you can write a simple wrapper by yourself.

### Better User experience
* Svelte surgically updates the DOM without relying on a middle interpreter or any complex reconciliation algorithm. It’s often beating out other frameworks on UI and [JS speed](https://krausest.github.io/js-framework-benchmark/current.html). And it’s increasingly used in production on various popular and traction heavy sites.
* React is fast enough to give a good user experience for the majority of use cases but when we have performance needs in terms of rendering, especially if we work around data visualization then it is not enough. [Virtual DOM is a pure overhead](https://svelte.dev/blog/virtual-dom-is-pure-overhead). It doesn’t matter how efficiently the diffing algorithm evolves; it will always eat up client’s hardware resources.

### Doesn’t Svelte have any flaws?
* The syntax used for component props is a bit weird for me.
* Svelte won’t listen for reference updates and array mutations, which developers need to actively look out for and make sure arrays are reassigned.
* Compared to Svelte, React has a huge community and you can easily find a [solution to any problem](https://stackoverflow.com/questions/68291908/why-is-promise-then-called-twice-in-a-react-component-but-not-the-console-log). As a React developer, I really like Svelte as well. However, I've only found Svelte useful in lightweight simple projects. I like Svelte API and reactivity out of the box, but for all its advantages, the ecosystem around svelte is lacking compared to react. To speed up development in a more complicated web app we need tools/libraries like [svelte kit](https://kit.svelte.dev/), svelte-query, svelte-yup etc., but these are not mature enough yet. Hopefully, Svelte's ecosystem will get better as more people start using it.

### Conclusion
1. If you have Javascript knowledge and experience in other frameworks, you can adapt to Svelte quickly. Coding in [Svelte feels effortless](https://www.htmlallthethings.com/podcasts/svelte-is-here-to-stay), development time is also blazing fast when compared to React or many other frameworks.
2. In my experience I observed that developers always find it difficult to switch from other frameworks to React Hooks. Trust me, I was a big fan of React hooks when it was introduced. I do like the idea of function components and hooks, but I certainly don’t enjoy working with them in a larger project.
3. See the below history, [React](https://twitter.com/Rich_Harris/status/1421544403882160136) [keeps evolving](https://mobile.twitter.com/leeerob/status/1353523847937536000) but in the [wrong direction](https://mobile.twitter.com/dan_abramov/status/1191495127358935040), Svelte simply just chooses the right path.

![React State](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1rp3zggih80eeqv3gi3k.jpeg)