Introducing Svelte 5: Reactivity With Runes

Aparna Jins
Impelsys
Published in
6 min readSep 3, 2024

Introduction

Svelte is a modern JavaScript framework and is gaining immense popularity among developers day by day. The Svelte team has announced the release of its new version: Svelte 5 and it is one of the most interesting framework releases in a long time. But what makes it so interesting?

With Svelte 5, the framework has taken a significant leap by introducing a more efficient compiler and highly optimized reactivity, making it easier and more reliable than ever to build web applications.

In this blog, we’ll discuss the challenges faced with traditional frameworks, key features of Svelte 5, explore the new ways of state management, props handling, and Effects.

Traditional Framework Challenges

It is important to choose the right framework for building our application since it plays a significant role in its performance and efficiency. Most frameworks are plagued by issues such as long coding structures, complex state management and performance issues.

Suboptimal user experiences, application delays, and long development times result from these factors, leading to the search for a more efficient and scalable framework while improving the development process.

Discovery

In search of the optimal framework, we found a variety of JavaScript frameworks, but Svelte stood out for its unique approach of devoting the majority of its work to the compiler. This resulted in a highly performant vanilla JavaScript that runs like a breeze. By reducing bundle size, increasing performance, and creating efficient runtime code, the compiled code updates the states directly in the dom, avoiding the need for a virtual dom. This addresses our problem by reducing bundle sizes, increasing performance, and creating efficient runtime code.

Advantages/Benefits

Svelte 5 introduces several benefits over the previous versions:

1. Improved Reactivity: The introduction of runes, which utilize signals under the hood, makes Svelte’s reactivity more robust and reliable. Developers can now manage complex states with greater confidence, knowing that the framework will handle changes efficiently and predictably.

2. Simplified State Management: Runes streamline state management, reducing the need for verbose and error-prone syntax. This not only makes the code cleaner but also enhances the developer experience, staying true to Svelte’s core philosophy.

3. Better TypeScript Integration: Svelte 5 brings tighter TypeScript integration, making it easier to work with generics and ensuring type safety across components. This improvement is especially valuable for large-scale applications where type safety is critical.

4. Backward Compatibility: Despite the introduction of new features, Svelte 5 maintains backward compatibility with Svelte 4. This allows developers to upgrade their applications incrementally without breaking existing code.

A New Approach to State Management with Runes

State management is one of the critical aspects of any framework. Before version 5, the reactivity mechanism in svelte components and svelte stores was different making it a little confusing.

Svelte 5 introduces a groundbreaking new feature called “Runes,” named after the ancient symbols used for casting spells. In Svelte 5, Runes are functions powered by signals that unify reactivity across both Svelte and TypeScript files.

State in Svelte 5

Previously in Svelte, the state was declared using the let keyword and the derived state was handled with $: syntax.

While this worked, there is a chance it would break into complex scenarios. Svelte 5 replaces this with a more robust and intuitive system using the $state and $derived runes.

Now you can declare and update your state more clearly.

The rune $state is used to create state variables while $derived is used on states to automatically update states based on their dependencies.

Time to say Goodbye to Stores

Stores were a popular way to manage the state outside of components in the previous versions of Svelte. This is how a code to convert temperature would look in Stores.

With the new version, we no longer need to maintain stores as we have Runes for unified state management. With Runes the same code would look like this:

It is much simpler and could be written in fewer lines. Even though stores are supported in Svelte 5, it is no longer necessary as we can use Runes outside the components.

Improved Props Handling

Props are an unexceptional part of the component-based architecture that helps communication between different components in an application. Previous versions used the keyword export to define props.

Svelte 5 makes props handling more flexible and typescript friendly. It comes up with a more elegant $props rune. It allows you to destructure your props and assign default values using $bindable rune.

Side Effects with $effect

In Svelte 4 you could run code for effects like:

Handling effects like DOM manipulation and API calls are made efficient in Svelte 5 using the $effect rune, which is an easier way to manage the side effects.

This specific rune runs after any changes are made in states and automatically updates the DOM in the correct order. You can also untrack your states, giving control over your application.

Interoperability with Svelte 4

While Svelte 5 is coming with a lot of features, how compatible is it with the current version?

You can use old components along with the new Svelte 5 components, but you are not allowed to mix the same syntax with new ones on a single component. This ensures you can adopt Svelte 5 features in your old Svelte application without refactoring your entire code base.

Drawbacks

While Svelte 5 offers numerous advantages, there are some challenges to consider:

1. Learning Curve: Developers familiar with Svelte 4 might need some time to adapt to the new runes system. The shift in syntax and concepts, while ultimately beneficial, may initially slow down development.

2. Mixed Syntax Challenges: The ability to mix Svelte 4 and Svelte 5 components is a double-edged sword. While it offers flexibility, it can also lead to confusion and potential bugs if not managed carefully.

Solution

To address the issues of long coding structures, complex state management, and performance issues, we integrated Svelte into our development stack. This transition involved rewriting key components to leverage Svelte’s compile-time optimizations and reactive state management.

The immediate results included faster load times, a more responsive user interface, and a significant reduction in code complexity. The streamlined development process also allowed our team to focus more on feature development and less on debugging and optimization.

Conclusion

Svelte 5 is a huge improvement in web development making your applications more efficient, reliable and simple. With new features like Runes for state management and props management, this new version can be a game changer in the modern framework universe.

Svelte 5 is a good choice for both beginners and experienced developers. It improves developer experience, and reliability and enhances performance, making it the most accurate choice for building fast, lightweight and maintainable applications. To know more about Runes, refer to this official blog from the Svelte team.

--

--