I found myself needing to copy/paste my personal custom stores a lot, might as well make it public.
This Svelte library gives you two (and more coming) custom Writable
's.
npm install @n-vh/svelte-writable
customWritable(value, callback)
Create a custom Writable
that allows you to add custom methods to a store.
const store = customWritable(1, (store) => ({
increment() {
store.update((n) => n + 1);
},
power(n: number) {
store.update((value) => value ^ n);
},
}));
store.increment();
// $store = 2
store.power(5);
// $store = 32
localStorageWritable(key, initialValue)
Synchronize your Writable
with localStorage
.
const store = localStorageWritable('count', 0);
store.set(1);
// $store = 1
// localStorage.count = 1
// manually change localStorage
localStorage.count = 2;
// $store = 2
// localStorage.count = 2
Consider opening an Issue or a Pull Request!
© 2023