0 day streak
javascript / beginner
Snippet

Writable Stores

Writable stores allow you to manage state that can be accessed and modified from any component in your application, serving as a simple global state management tool.

snippet.js
javascript
1
2
3
4
5
6
7
import { writable } from 'svelte/store';
export const count = writable(0);
// Update value
count.set(10);
count.update(n => n + 1);
svelte
Breakdown
1
import { writable } from 'svelte/store';
Imports the function to create a writable store from the Svelte store module.
2
export const count = writable(0);
Creates a store with an initial value of 0 and exports it for use in other files.
3
count.update(n => n + 1);
Increments the current value of the store by 1 using a callback function.