Skip to content

Commit 58cc386

Browse files
committed
docs(store): improve onChange and effects examples & add more detail
1 parent 5c9fcc5 commit 58cc386

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

apps/docs/pages/store/onChange-and-effects.mdx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,17 @@ const unsubscribe = userStore.name.onChange(console.log);
4848
Specify `deps` to react only to certain state parts. It can be a keys array or a function returning dependencies.
4949

5050
```tsx
51-
const unsubscribe = store.onChange(callback, { deps: ['num'] });
51+
const unsubscribe = store.onChange(callback, { deps: ['name '] });
5252
```
5353

54+
```tsx
55+
const unsubscribe = store.onChange(callback, {
56+
deps: (state) => [state.name],
57+
});
58+
```
59+
60+
You can subscribe to deeply nested changes inside the deps callback eg `state => [state.user.address.street]`
61+
5462
#### Immediate Invocation
5563

5664
Use `fireImmediately` to trigger the callback immediately with the current state.
@@ -107,3 +115,27 @@ countStore.unsubscribeFromEffects();
107115
```
108116

109117
These methods are used under the hood of `createStoreContext` to make multiple instances of the store work correctly.
118+
119+
Here is a super simplified version of `createStoreContext` to show how the effects are unsubscribed from when the component is unmounted
120+
121+
```tsx
122+
export function createStoreContext(store) {
123+
const Provider = () => {
124+
const storeInstance = React.useRef(store.create(localInitialValue));
125+
126+
React.useEffect(() => {
127+
return () => {
128+
storeInstance.current.unsubscribeFromEffects();
129+
};
130+
}, []);
131+
132+
return (
133+
<Context.Provider value={storeInstance.current}>
134+
{children}
135+
</Context.Provider>
136+
);
137+
};
138+
}
139+
```
140+
141+

packages/store/README.md

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,23 +50,24 @@ function Controls() {
5050
const counterStore: StoreApi<number, {}>;
5151
```
5252

53-
### Example with computed properties and actions
53+
### Advanced example
5454

5555
```tsx
5656
import { store } from '@davstack/store';
5757

5858
const counterStore = store()
5959
.state({ count: 0 })
60-
.computed((store) => ({
61-
doubleCount: () => store.count.use() * 2,
62-
}))
6360
.actions((store) => ({
6461
increment: () => store.count.set(store.count.get() + 1),
6562
decrement: () => store.count.set(store.count.get() - 1),
63+
}))
64+
.computed((store) => ({
65+
doubleCount: () => store.count.use() * 2,
66+
}))
67+
.effects((store) => ({
68+
logChanges: () => store.onChange(console.log),
6669
}));
6770

68-
counterStore.onChange(console.log);
69-
7071
function Counter() {
7172
const count = counterStore.count.use();
7273
return <div>Count: {count}</div>;
@@ -84,13 +85,16 @@ function Controls() {
8485
// Generated types
8586
const counterStore: StoreApi<
8687
{ count: number },
87-
ComputedMethods<{
88-
doubleCount: () => number;
89-
}> & {
88+
{
9089
increment: () => void;
9190
decrement: () => void;
92-
}
91+
} & ComputedMethods<{
92+
doubleCount: () => number;
93+
}> & EffectMethods<{
94+
logChanges: () => UnsubscribeFn;
95+
}>;
9396
>;
97+
9498
```
9599

96100
Note: store(initialValue) and store.state(initialValue) are equivalent, it's just a matter of preference.
@@ -155,3 +159,4 @@ Contributions are welcome! Please read our [contributing guide](link-to-contribu
155159
### License
156160

157161
This project is licensed under the [MIT License](link-to-license). See the LICENSE file for details.
162+

0 commit comments

Comments
 (0)