Getting Started with Svelte from React Developer Perspective, with Code Comparison
Svelte is an open-source component-based javascript framework to build user interfaces. But unlike traditional frameworks like React or Vue, which perform much of their work in the browser (with Virtual DOM), Svelte shifts that work into a compile step that happens in build process.
By compiling components into optimized code that directly manipulates the DOM, Svelte apps run faster and are more responsive than those built with other frameworks.
Additionally, Svelte reduces the amount of boilerplate code developers need to write, leading to increased productivity and easier maintenance. The framework also offers a reactive programming model out of the box, which simplifies state management and makes it easier to build complex, dynamic user interfaces.
Svelte’s popularity has been steadily increasing since its release. It has gained a significant following due to its innovative approach and the enthusiastic support from its community. The framework is frequently praised for its simplicity and the positive developer experience it provides.
On the recent StackOverflow survey 2024, Svelte becomes one of the most admired and desired web frameworks.
The future prospects for Svelte look promising, as more developers and companies recognize its potential for building high-performance web applications. Notable companies that have adopted Svelte include The New York Times, for interactive content, and Apple, for parts of their web infrastructure.
With its strong community support, continuous development, and growing ecosystem of tools and libraries, Svelte is well-positioned to become a mainstream choice for front-end development in the coming years.
How to Start Svelte Project in 2024
There are 2 ways to start a Svelte Project from scratch
1. Using Vite + Svelte Template
If you’re new to Svelte, it’s recommended to start with the Vite + Svelte template to learn the fundamentals.
This will set up a basic Svelte project using Vite, giving you a solid foundation to learn Svelte’s core concepts.
2. Using SvelteKit
SvelteKit is a framework for building web applications with Svelte, providing a more advanced setup for routing, server-side rendering, and more. Its like Next.js for React.
Starting with Vite + Svelte is great for exploring and understanding Svelte fundamentals. Once comfortable, you can move to SvelteKit for more complex, real-world applications.
Related Post
Svelte Codes, compared to React
1. Component
This Svelte component defines a name
variable, styles a heading, and uses the variable in the markup. The <script>
tag is used for JavaScript, the <style>
tag for CSS, and the markup is written directly in the component.
Here is the equivalent codes in React:
2. State and Reactivity
In Svelte, state and reactivity are handled directly within the component. The count
variable is reactive, and the increment
function updates it. The on:click
directive is used to handle the click event.
Here is the equivalent codes in React:
3. Props
In Svelte, props are passed to child components using attributes. The export let
syntax is used in the child component to declare props.
Here is the equivalent codes in React:
4. DOM Event
Svelte uses the on:click
directive to handle DOM events.
Here is the equivalent codes in React:
5. Component Custom Event
In Svelte, custom events are dispatched from child components using createEventDispatcher
, and parent components handle these events using the on:eventName
directive.
Here is the equivalent codes in React:
6. Conditional Rendering
Svelte uses {#if}
and {/if}
blocks for conditional rendering.
Here is the equivalent codes in React:
Related post
7. List Rendering
Svelte uses {#each}
and {/each}
blocks for list rendering.
Here is the equivalent codes in React:
8. Await Block
Svelte’s {#await}
block handles promises directly in the markup.
Here is the equivalent codes in React:
9. Form Binding
Svelte’s bind:value
directive provides two-way data binding for form inputs.
Here is the equivalent codes in React:
10. Lifecycle
Every component has a lifecycle that starts when it is created, and ends when it is destroyed.
Here is the equivalent codes in React:
11. Stores (Global state management)
Svelte has built-in global-state management with writable stores. The $
prefix is used to subscribe to the store. Beside stores
, Svelte also has Context API, which is similar to React Context API.
As you can see, Svelte codes are mostly more straightforward than React for the same functionality. For me, its like a simplified version of Vue. The event and directive is similar with Vue.
However, We’ve just explored the basic syntaxes for Svelte. There are a lot more to learn if you really want to use it for real projects. You can learn more about it through the official docs with interactive tutorial.
Svelte VS React comparison table
Feature | Svelte | React |
---|---|---|
Component Syntax | Uses single-file components with .svelte | Uses JSX in .js or .jsx files |
State Management | Local state handled directly in components, stores for global state | useState and useReducer for local state, Context API, and external libraries like Redux for global state |
Reactivity | Automatic reactivity with direct assignments | Explicit state updates with hooks |
Event Handling | Uses on:event directives for DOM and custom events | Uses onEvent attributes for DOM events, props for custom events |
Conditional Rendering | {#if} and {/if} blocks | JavaScript conditional expressions |
List Rendering | {#each} and {/each} blocks | JavaScript map function |
Form Binding | bind:value for two-way data binding | Controlled components with value and onChange attributes |
Lifecycle Methods | onMount , beforeUpdate , afterUpdate , | useEffect , componentDidMount , componentDidUpdate , componentWillUnmount |
Performance | Compiles to highly optimized imperative code, resulting in faster runtime performance and smaller bundle sizes | Uses virtual DOM to optimize updates, but can result in larger bundle sizes and slightly slower runtime performance compared to Svelte |
Learning Curve | Easier for beginners, simpler syntax and concepts | Steeper learning curve, requires understanding of JSX and hooks |
Tooling | SvelteKit for full-featured applications, Vite for simple setups | Create React App, Next.js for server-side rendering and static site generation |
Popularity | Growing, smaller community | Widely adopted, large community and ecosystem |
File Size | Smaller bundle sizes due to compiled code | Larger bundle sizes, depends on the size of the library and dependencies |
Ecosystem | Fewer libraries and plugins | Rich ecosystem with numerous libraries and plugins available |
First-Party Support | SvelteKit for advanced applications | Next.js, Create React App for application scaffolding |
Conclusion
Svelte and React are both powerful tools for building modern web applications, each with its unique strengths. Svelte stands out with its straightforward syntax, automatic reactivity, smaller bundle sizes and faster runtime performance. Its simplicity makes it an excellent choice for beginners and developers looking for a lightweight solution.
React, on the other hand, offers a rich ecosystem, extensive libraries, and robust tooling options. Its use of the virtual DOM optimizes updates, although it can lead to larger bundle sizes and a steeper learning curve. Ultimately, the choice between Svelte and React depends on the specific needs of your project and your familiarity with each framework.
Do you have your own opinion about this? Don’t hesitate to share it in the comment below. If you find this article helpful, kindly give it a like and share :smile:.
Thank you. Happy coding!