https://github.com/nasso/svelte-bound-store
https://github.com/nasso/svelte-bound-store
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/nasso/svelte-bound-store
- Owner: nasso
- License: mit
- Created: 2023-08-31T18:14:36.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2023-09-14T16:35:30.000Z (almost 3 years ago)
- Last Synced: 2025-06-15T09:17:31.572Z (about 1 year ago)
- Language: JavaScript
- Size: 48.8 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `svelte-bound-store`
`bound` is like `derived`, but the function returns another store.
## Usage
```js
import { writable } from "svelte/store";
import { bound } from "svelte-bound-store";
const foo = writable("foo");
const bar = writable("bar");
const baz = writable("baz");
const state = writable({
index: 0,
items: [foo, bar, baz],
});
const current = bound(state, ({ index, items }) => items[index]);
current.subscribe(console.log); // "foo"
foo.set("foo2"); // "foo2"
// change the index
state.update((state) => ({ ...state, index: 1 })); // "bar"
foo.set("foo3"); // logs nothing because `current` is no longer bound to `foo`
```