Introducing Svelte 5 - What To Expect
Introducing Svelte 5 - What To Expect
that instruct the Svelte compiler on how to handle reactivity in components and now, for
the first time, in JavaScript and TypeScript modules as well. These runes will offer
developers a more straightforward and explicit way to manage reactive variables within
their code.
Svelte 5 introduces a shift in the reactivity model. The 'state' rune marks values as
reactive, which is a departure from the implicit reactivity of top-level 'let' declarations.
This change aims to make the reactivity model more explicit and predictable.
Reactivity Model
One of the most significant changes in Svelte 5 is the shift in the reactivity model. The
'state' rune marks values as reactive, a departure from the implicit reactivity of top-level
'let' declarations. This change aims to make the reactivity model more explicit and
predictable.
<script>
import { state } from 'svelte';
let count = state(0); // `state` rune makes `count` reactive
function increment() {
count.set(c => c + 1); // use `set` to update the state
}
</script>
<button on:click={increment}>
Clicks: {$count}
</button>
Svelte 5 addresses derived values with a 'derived' rune, which ensures that derived values
are recalculated lazily and kept in sync. This lazy calculation is a step towards making the
framework more type-safe and maintaining consistent behaviour across component logic.
<script>
import { state, derived } from 'svelte';
firstName, lastName
,(
$firstName, $lastName
<script>
import { state, effect } from 'svelte';
function useCounter() {
let count = state(0);
function increment() {
count.set(c => c + 1);
}
effect(() => {
console.log(`The count is: ${$count}`);
});
We use cookies
returnto {improve
count,yourincrement
browsing experience.
}; By continuing to use this
website,} you agree to our use of cookies.
const { count, increment } = useCounter();
</script>
<button on:click={increment}>
Count: {$count}
</button>
<script>
import { state, effect } from 'svelte';
function useCounter() {
let count = state(0);
function increment() {
count.set(c => c + 1);
}
effect(() => {
console.log(`The count is: ${$count}`);
});
<script>
import { state } from 'svelte';
<script lang="ts">
import { props } from 'svelte';
interface MyProps {
name: string;
age: number;
}
Conclusion