https://github.com/captaincodeman/svelte-state-store
https://github.com/captaincodeman/svelte-state-store
Last synced: 9 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/captaincodeman/svelte-state-store
- Owner: CaptainCodeman
- Created: 2023-11-06T16:20:49.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-04-27T18:39:04.000Z (over 1 year ago)
- Last Synced: 2025-04-04T10:36:31.501Z (10 months ago)
- Language: JavaScript
- Size: 50.8 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# svelte-state-store
A [Svelte writable store](https://svelte.dev/docs/svelte-store#writable) that allows immediate access to the current state via a `value` getter.
This avoids the need to use the less efficient `get(store)` and to access the current store value without having to setup a subscription which can sometimes be awkward.
It can be useful where you need a store value but it isn't a dependency that would need to trigger an update or you would otherwise need to `get` the value inside a tight loop (e.g. a mouseover handler checking for the pointer being over objects that are within a store, to set the highlighted one). In certain situations it can help create simpler more efficient code.
## Usage
Install using your package-manager of choice, e.g.
pnpm i -D svelte-state-store
Import and use just as you would a regular `writable` store:
```ts
import { state } from 'svelte-state-store';
export const my_store = state(123);
```
You can now use the store as a regular `writable` store, subscribing to it for updates, and using it with `derived` stores, but you can also access the current value outside of a subscription using:
```ts
console.log(my_store.value); // 123
```
You can pass a `StartStopNotifier` to the store, just as with a regular Svelte `writable` store.
If you need to define a type for your store, use `State` where you would have used `Writable`:
```ts
import type { Writable } from 'svelte/store`
function that_uses_store(store: Writable) { }
```
becomes ...
```ts
import type { State } from 'svelte-state-store`
function that_uses_store(store: State) { }
```