Skip to content

Commit d46e617

Browse files
christianalfonigitbook-bot
authored andcommitted
GitBook: [master] 4 pages modified
1 parent 7c7267d commit d46e617

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

SUMMARY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
* [API](api/README.md)
1212
* [app](api/app.md)
1313
* [action](api/action.md)
14+
* [computed](api/computed.md)
1415
* [derived](api/derived.md)
1516
* [namespaces](api/namespaces.md)
1617
* [providers](api/providers.md)
18+
* [reaction](api/reaction.md)
1719
* [state](api/state.md)
1820

api/computed.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# computed
2+

api/reaction.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# reaction
2+

reactions.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,62 @@
11
# Reactions
22

3+
There are times you want to translate a state change into a side effect. Maybe you want to store some data to local storage when a certain state path is mutated. Or maybe inside a component you want to change its internal state or pass data to some UI plugin related to a state path change.
4+
5+
### Application reactions
6+
7+
You can define your reactions at the application level. These reactions will run as long as the application lives.
8+
9+
```javascript
10+
import App from 'overmind/$VIEW'
11+
12+
const app = new App({
13+
state: {
14+
foo: 'bar'
15+
},
16+
reactions: (reaction, action) => {
17+
const storeFooInStorage = action()
18+
.do((_, { storage, state }) => storage.set('foo', state.foo))
19+
20+
return {
21+
storeFoo: reaction(state => state.foo, storeFooInStorage)
22+
}
23+
}
24+
})
25+
```
26+
27+
{% hint style="info" %}
28+
What to take notice of is that if the returned state of the reaction was an array or object, any change within that array/object would cause the reaction to run again.
29+
{% endhint %}
30+
31+
### Component reactions
32+
33+
Component reactions differ in the sense that they only live through the component lifecycle. They are unregistered when the component unmounts. Also you are likely to not use the actions passed to the second callback.
34+
35+
{% tabs %}
36+
{% tab title="React" %}
37+
```javascript
38+
import React from 'react'
39+
import { connect } from '../app'
40+
41+
class MyComponent extends React.Component {
42+
componentDidMount () {
43+
this.props.app.reaction(
44+
'focusInputOnFooChange',
45+
state => state.foo,
46+
() => this.input.focus()
47+
)
48+
}
49+
render () {
50+
return <input ref={node => { this.input = node} />
51+
}
52+
}
53+
54+
export default connect(MyComponent)
55+
```
56+
{% endtab %}
57+
58+
{% tab title="Vue" %}
59+
60+
{% endtab %}
61+
{% endtabs %}
62+

0 commit comments

Comments
 (0)