Skip to content

Commit

Permalink
Merge pull request #151 from yezyilomo/update_docs
Browse files Browse the repository at this point in the history
Update documentation
  • Loading branch information
yezyilomo authored Dec 25, 2022
2 parents 2417405 + 60ed129 commit 8d4a61c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
12 changes: 11 additions & 1 deletion website/docs/api_reference/high_level_api/store.subscribe.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ store.getState(key).subscribe({
})
})
```
With this observer function will only be called when the selected state changes
With this observer function will only be called when the selected state changes.


Another way to subscribe to nested state or derived state is to call `select` on a global state then subscribe to it as

```js
store.getState(key).select(state => selected_state).subscribe(value =>{
// Do your thing here
}
)
```
21 changes: 19 additions & 2 deletions website/docs/api_reference/low_level_api/createGlobalState.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ Some of the methods available in a global state object are `getValue`, `updateVa
// Signature
globalState.subscribe(observer: Function | Subscription: {observer, selector});
```
- `select`: This is used to derive another state or select a deeply nested state
```js
// Signature
globalState.select(selector: Function);
```
This returns `DerivedGlobalState` that you can subscribe to by calling `subscribe` on it as
```js
// Signature
globalState.select(selector: Function).subscribe(observer: Function);
```

Below is an example showing all of them in action
```js
Expand All @@ -51,12 +61,19 @@ count.getValue() // This will give 0

count.setValue(1) // This set the value of count to 1

count.updateValue(val => val+1) // This will increment the value of count

// This will print whenever count change
const unsubscribe = count.subscribe(val => console.log(val))

unsubscribe() // This will unsubscribe the observer above

// An example for nested state
const user = createGlobalState({name: "Yezy", weight: 65});

user.updateValue(user => {user.weight += 1}) // This will increment the weight

// Select user name and subscribe to it,
// this will be printing whenever user name changes
user.select(user => user.name).subscribe(name => console.log(name));
```


Expand Down
2 changes: 1 addition & 1 deletion website/docs/basic_concepts/derived_state.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sidebar_position: 3
---

# Derived & Nested State
With state pool you can subscribe to deeply nested global state or a derived state. Both `store.useState` and `store.useReducer` accepts an optional configuration parameter with which you can pass `selector` & `patcher` options that are used to derive and update state.
With state pool you can subscribe to deeply nested or derived state. Both `store.useState` and `store.useReducer` accepts an optional configuration parameter with which you can pass `selector` & `patcher` options that are used to derive and update state.

Here is a simple example showing how to use `selector` & `Patcher` options

Expand Down
12 changes: 11 additions & 1 deletion website/docs/basic_concepts/managing_subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ store.getState(key).subscribe({
})
})
```
With this, observer function will only be called when the selected state changes
With this, observer function will only be called when the selected state changes.


Another way to subscribe to nested state or derived state is to call `select` on a global state then subscribe to it as

```js
store.getState(key).select(state => selected_state).subscribe(value =>{
// Do your thing here
}
)
```

0 comments on commit 8d4a61c

Please sign in to comment.