context-scoped-state 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,10 +32,11 @@ pnpm add context-scoped-state
|
|
|
32
32
|
|
|
33
33
|
## Quick Start
|
|
34
34
|
|
|
35
|
-
### 1.
|
|
35
|
+
### 1. Create Your Store (one file, one export)
|
|
36
36
|
|
|
37
37
|
```tsx
|
|
38
|
-
|
|
38
|
+
// stores/counterStore.ts
|
|
39
|
+
import { Store, createStoreHook } from 'context-scoped-state';
|
|
39
40
|
|
|
40
41
|
class CounterStore extends Store<{ count: number }> {
|
|
41
42
|
protected getInitialState() {
|
|
@@ -50,19 +51,16 @@ class CounterStore extends Store<{ count: number }> {
|
|
|
50
51
|
this.setState({ count: this.getState().count - 1 });
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
### 2. Create the Hook
|
|
56
54
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const useCounterStore = createStoreHook(CounterStore);
|
|
55
|
+
// This single export is all you need
|
|
56
|
+
export const useCounterStore = createStoreHook(CounterStore);
|
|
61
57
|
```
|
|
62
58
|
|
|
63
|
-
###
|
|
59
|
+
### 2. Use in Your App
|
|
64
60
|
|
|
65
61
|
```tsx
|
|
62
|
+
import { useCounterStore } from './stores/counterStore';
|
|
63
|
+
|
|
66
64
|
function Counter() {
|
|
67
65
|
const store = useCounterStore();
|
|
68
66
|
|
|
@@ -75,7 +73,6 @@ function Counter() {
|
|
|
75
73
|
);
|
|
76
74
|
}
|
|
77
75
|
|
|
78
|
-
// Wrap with Context provider
|
|
79
76
|
function App() {
|
|
80
77
|
return (
|
|
81
78
|
<useCounterStore.Context>
|
|
@@ -85,7 +82,7 @@ function App() {
|
|
|
85
82
|
}
|
|
86
83
|
```
|
|
87
84
|
|
|
88
|
-
That's it.
|
|
85
|
+
That's it. One hook export gives you the hook and its `.Context` provider. No extra setup needed.
|
|
89
86
|
|
|
90
87
|
## Examples
|
|
91
88
|
|
|
@@ -190,8 +187,8 @@ No mocking libraries. No global state cleanup. Just render with the state you ne
|
|
|
190
187
|
|
|
191
188
|
**vs useState:**
|
|
192
189
|
|
|
193
|
-
- `useState` binds state
|
|
194
|
-
-
|
|
190
|
+
- `useState` binds state directly to the component — poor separation of concerns and hard to test since you can't easily set a component to a specific state
|
|
191
|
+
- Lifting state up with `useState` requires refactoring components and passing props; with `context-scoped-state`, just move the Context wrapper up the tree
|
|
195
192
|
|
|
196
193
|
**vs useReducer:**
|
|
197
194
|
|