DEV Community

Cover image for Svelte VS React, which one?
Dima Odintsov
Dima Odintsov

Posted on • Edited on

Svelte VS React, which one?

React is a web framework for rendering web components written by Facebook.
Svelte is a web front-end framework using web components under the hood.
The idea behind these two frameworks are to help web developers use components more efficiently.
This article will compare how React and Svelte approach this problem of component creation, what problems they are trying to solve, etc.

The main difference between React and Svelte is what they are trying to solve.
React focuses on components, while Svelte provides components optimization. A component in web development is basically a web page or web app that can be reused many times. Components use HTML, CSS and JavaScript code combined to create the web page/app that you want. This allows developers to minimize coding because you can reuse components as much as possible throughout the website/web app you're developing.

Another difference between these two frameworks is how they implement components within their framework's code structure. In React, components are created using JSX syntax which looks similar to template language but with JavaScript embedded so it would look something like this:

<MyComponent>Hello World</MyComponent>

This JSX code that's component is wrapped in an internal function so when components are rendered, these components are written to the DOM at run time. The component getting created in this internal function is just for convenience, it doesn't actually do anything until it's instantiated (or called) but components can be instantiated anywhere in the web app/website - they don't have to be written within this internal functions. This process of writing HTML tags inside JavaScript functions goes against what React stands for which is avoiding any 'stateful' Javascript because altering these components (or HTML elements) would alter this internal function's state which changes how components are rendered, so React components are immutable.

Svelte components are written in HTML view language which looks similar to CSS but with JavaScript embedded so components would look something like this:

<my-component>Hello World</my-component>

Svelte components don't have any internal functions written according to the framework's documentation. This makes svelte components reusable just like normal components that developers create themselves without using a web front-end framework because Svelte component code is pure Web code. The benefit of using Svelte over creating pure Components on your own is not having to write/write again HTML tags for each. With Svelte you can write one component and Svelte components are all compiled to components at run time which is how web front-end frameworks work, but the difference between writing components on your own or using svelte is that with svelte components, they are automatically optimized for performance because Svelte compile away unused code so you don't have to worry about cleaning up these optimizations yourself when developing websites/web apps. This makes Svelte perfect for big web applications that use a lot of components.

Another clear benefit of Svelte is how much easier it is to achieve code reusability than React because in React web page layouts often need to be written in JSX and this can get messy pretty quickly. With Svelte, web page layouts can be written in HTML which is easier to maintain.

Svelte are compiled to components at run time so this is where Svelte provides web-component optimization by compiling away unused code. React components are created with internal functions according to the framework's documentation and these components are immutable meaning you cannot delete/remove any of the HTML tags within the component or remove any JavaScript that was initially used when creating the component because they would change how things rendered (HTML elements/tags) and therefore alter what's in the DOM which renders how these components look on a website. This process of writing HTML tags inside JavaScript functions goes against what React stands for which is avoiding any 'stateful' Javascript. With React components, web page layouts need to be written in JSX which gets messy pretty quickly because there's a lot of components that need multiple web page layouts and then you have to write web page layout code inside each component's internal functions - there's a high chance of overlooking something important when writing web page layout code inside these internal functions.

Although Svelte has a steeper learning curve than React, it can do what React can do and more so svelte is favored by web developers who are concerned about how much time they spend on programming because less time spent programming means more time developing websites/web apps while still achieving the same goals.

Conclusion: This article's comparison between svelte and react components provides web developers who could be interested in Svelte components (but might not know much about it) with a clear explanation of what these components can do compared to React components. The decision on which one is the best between Svelte and React components depends on where web developers want to spend their time while developing websites/web apps: if they want to spend more time programming than cleaning up code/improving performance, then Svelte is their best choice; otherwise, if they prefer spending more time programming but would also like clean code that's easy to maintain and good performance because HTML web pages can get messy very easily when components are involved then React is their best choice.

FAQ:

Q) What web front-end framework is Svelte components compatible with?

A) Svelte components have no compatibility issues because they compile to components which means they can be used on any web page regardless of what web-front end framework (React, Angular, Vue) the web page is using.

Q) What web front-end framework is React components compatible with?

A) React components can only be used on web pages that use React web-front end frameworks.

Q) Why would web developers choose Svelte over React?

A) Svelte components are compiled at run time meaning they provide web developers with benefits such as being able to optimize how their website/web app appears because unused code will not be included in the final product which means there's less of a risk for bugs while developing websites/web apps if these optimizations were developed by hand. Another reason why web developers would choose Svelte components is because it has deeper learning curve than React so web developers can spend more time web programming and less time web cleaning up code/improving performance.

Q) Why would web developers choose React over Svelte?

A) web developers who prefer spending more time web programming than web cleaning up code/optimizing performance because it's easier to do so when writing React components compared to creating these components by hand or using a different framework that compiles into components at run time. Another advantage of using React is that these components are compatible with other frameworks such as Angular, Vue, Ember etc. whereas Svelte doesn't work with other frameworks at all due to their compilation process which means you have to create your website/web app from scratch using components that React components can be used with.

Make sure to visit my portfolio :)

Cheers!
Thanks for reading! ❤️

Top comments (1)

Collapse
 
anakut profile image
muh ali

long live svelte