|
| 1 | +--- |
| 2 | +title: Simple store 💾 |
| 3 | +description: A reactive store that combines the simplicity of signals with the power of "selectors" you'd find in Zustand or Redux. |
| 4 | +--- |
| 5 | + |
| 6 | +A reactive store that combines the simplicity of signals with the power of "selectors" you'd find in Zustand or Redux. |
| 7 | + |
| 8 | +```tsx |
| 9 | +import { store } from "@simplestack/store"; |
| 10 | + |
| 11 | +const documentStore = store({ |
| 12 | + title: "Untitled", |
| 13 | + description: "Description", |
| 14 | +}); |
| 15 | + |
| 16 | +const title = documentStore.select("title"); |
| 17 | +const description = documentStore.select("description"); |
| 18 | + |
| 19 | +title.set("New title"); |
| 20 | +console.log(title.get()); // "New title" |
| 21 | +description.set("New description"); |
| 22 | +console.log(description.get()); // "New description" |
| 23 | +``` |
| 24 | + |
| 25 | +## Installation |
| 26 | + |
| 27 | +Install the dependency from npm: |
| 28 | + |
| 29 | +```bash |
| 30 | +npm i @simplestack/store |
| 31 | +``` |
| 32 | + |
| 33 | +Then, import the store and use it in your component: |
| 34 | + |
| 35 | +```tsx |
| 36 | +import { store } from "@simplestack/store"; |
| 37 | +``` |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +Here's an overview of how stores are created, and how you can operate on parts of a store using `.select()`: |
| 42 | + |
| 43 | +```tsx |
| 44 | +import { store } from "@simplestack/store"; |
| 45 | +import { useStoreValue } from "@simplestack/store/react"; |
| 46 | + |
| 47 | +// Define your store with an initial state |
| 48 | +const documentStore = store({ |
| 49 | + title: "Untitled", |
| 50 | + authors: ["Ada", "Ben"], |
| 51 | + meta: { |
| 52 | + pages: 3, |
| 53 | + tags: ["draft", "internal"], |
| 54 | + }, |
| 55 | +}); |
| 56 | + |
| 57 | +// Select parts of a store to listen to individually |
| 58 | +const titleStore = documentStore.select("title"); |
| 59 | +const tagsStore = documentStore.select("meta").select("tags"); |
| 60 | + |
| 61 | +function Document() { |
| 62 | + // Update your UI with the store's current state |
| 63 | + const { title, tags } = useStoreValue(documentStore); |
| 64 | + return ( |
| 65 | + <div> |
| 66 | + {title} {tags.join(", ")} |
| 67 | + </div> |
| 68 | + ); |
| 69 | +} |
| 70 | + |
| 71 | +function Title() { |
| 72 | + // And scope updates with selected stores for fine-grained control |
| 73 | + const title = useStoreValue(titleStore); |
| 74 | + return ( |
| 75 | + <input value={title} onChange={(e) => titleStore.set(e.target.value)} /> |
| 76 | + ); |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## API |
| 81 | + |
| 82 | +### store(initial) |
| 83 | + |
| 84 | +Creates a store with `get`, `set`, `subscribe`, and (for objects and arrays) `select`. |
| 85 | + |
| 86 | +- Parameters: `initial: number | string | boolean | null | undefined | object` |
| 87 | +- Returns: `Store<T>` where `T` is inferred from `initial` or supplied via generics |
| 88 | + |
| 89 | +```ts |
| 90 | +import { store } from "@simplestack/store"; |
| 91 | + |
| 92 | +const counter = store(0); |
| 93 | +counter.set((n) => n + 1); |
| 94 | +console.log(counter.get()); // 1 |
| 95 | + |
| 96 | +// Select parts of a store for objects and arrays |
| 97 | +const doc = store({ title: "x" }); |
| 98 | +const title = doc.select("title"); |
| 99 | +``` |
| 100 | + |
| 101 | +### React |
| 102 | + |
| 103 | +#### useStoreValue(store) |
| 104 | + |
| 105 | +React hook to subscribe to a store and get its current value. |
| 106 | + |
| 107 | +- Parameters: `store: Store<T> | undefined` |
| 108 | +- Returns: `T | undefined` |
| 109 | + |
| 110 | +```tsx |
| 111 | +import { store } from "@simplestack/store"; |
| 112 | +import { useStoreValue } from "@simplestack/store/react"; |
| 113 | + |
| 114 | +const counterStore = store(0); |
| 115 | + |
| 116 | +function Counter() { |
| 117 | + const counter = useStoreValue(counterStore); |
| 118 | + return ( |
| 119 | + <button onClick={() => counterStore.set((n) => n + 1)}>{counter}</button> |
| 120 | + ); |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## Type Reference |
| 125 | + |
| 126 | +These types are exported for TypeScript users. |
| 127 | + |
| 128 | +- StateObject: `Record<string | number | symbol, any>` |
| 129 | +- StatePrimitive: `string | number | boolean | null | undefined` |
| 130 | +- Setter<T>: `T | ((state: T) => T)` |
| 131 | +- Store<T>: |
| 132 | + - `get(): T` |
| 133 | + - `set(setter: Setter<T>): void` |
| 134 | + - `subscribe(callback: (state: T) => void): () => void` |
| 135 | + - `select(key: K): Store<SelectValue<T, K>>`: present only when `T` is an object or array |
| 136 | + |
| 137 | +## Contributing |
| 138 | + |
| 139 | +Contributions are welcome! Please feel free to submit an issue or pull request. |
| 140 | + |
| 141 | +## License |
| 142 | + |
| 143 | +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details. |
0 commit comments