Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vkurko/svelte-store2
https://github.com/vkurko/svelte-store2
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/vkurko/svelte-store2
- Owner: vkurko
- License: mit
- Created: 2021-03-24T09:32:27.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-16T10:38:17.000Z (almost 2 years ago)
- Last Synced: 2024-04-23T13:13:44.368Z (8 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-svelte-stores - svelte-store2
README
# Svelte store2
Sometimes you need to get the value of a store outside of a component or store that you are not subscribed to. Svelte provides a [get()](https://svelte.dev/docs#run-time-svelte-store-get) function for this, but it works by creating a temporary subscription, which may not be what you expect.Store2 provides `writable2`, `derived2` and `readable2` stores, each with a `get()` method to retrieve the current store value.
## Usage
Install the package:
```bash
npm install --save-dev svelte-store2
```
Then you can just replace the original Svelte stores with `writable2`, `derived2` or `readable2` and benefit from using the `get()` method:
```js
import {writable2, derived2} from 'svelte-store2';const w = writable2(0);
const d = derived2(w, $w => $w * 2);w.get(); // 0
d.get(); // 0w.set(2);
w.get(); // 2
d.get(); // 4
```