Part 1 / Stores / Custom stores
As long as an object correctly implements the subscribe method, it's a store. Beyond that, anything goes. It's very easy, therefore, to create custom stores with domain-specific logic.
For example, the count store from our earlier example could include increment, decrement and reset methods and avoid exposing set and update:
function createCount() {
const { subscribe, set, update } = writable(0);
return {
subscribe,
increment: () => update((n) => n + 1),
decrement: () => update((n) => n - 1),
reset: () => set(0)
};
}1
2
3
4
5
6
7
8
9
10
<script>
import { count } from './stores.js';</script>
<h1>The count is {$count}</h1><button on:click={count.increment}>+</button><button on:click={count.decrement}>-</button><button on:click={count.reset}>reset</button>
initialising