Chapter 4: Stores

Svelte’s reactivity inside Svelte components is intuitive and easy. Because Svelte is a compiler, it can transform regular variable assignments into something the UI will rerender when it changes (as we saw in Chapter 2). Reactive assignments make derived or computed variables very easy (as we saw in Chapter3).

Outside of Svelte components, we have a different mental model, there’s no inherently connected UI layer to our code, and the Svelte compiler won’t touch code outside of Svelte files. But what if we also want the goodies of reactivity outside of Svelte components? What if we have global state that lives inside a JavaScript file that many components should react to when it changes? This is where Svelte stores come in. ...

Get Svelte: A Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.