0 day streak
javascript / beginner
Snippet

Updating Stores with Functions

The update method takes a callback function that receives the current value and returns the new value. This is useful for updates based on existing state.

snippet.js
javascript
1
2
3
4
5
import { count } from './stores.js';
function increment() {
count.update(n => n + 1);
}
svelte
Breakdown
1
import { count } from './stores.js';
Imports the store instance from another file.
2
count.update(n => n + 1);
Calls update with a function that adds 1 to the current value 'n'.