You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
Adds a dispatchable **action** to the Juicr. Specify the`actionName` and a`function` that returns the state changes. The `data` is passed in from the **dispatch** call as well as the current Juicr `_state`. For example:
61
61
```javascript
62
-
juicr.action('delete', ({ id },_state)=> {
63
-
return { items:_state.items.filter(t=>t.id!== id ) }
64
-
})
62
+
juicr.action('delete', ({ id },_state)=> {
63
+
return { items:_state.items.filter(t=>t.id!== id ) }
64
+
})
65
65
```
66
66
67
67
### `juicr.dispatch('actionName', data)`
68
68
Dispatches an **action** with `data` on your Juicr. For example:
Reacts to changes in the state and returns new state changes, essentially like a computed property. Similar to **listen** you can react to changes on a single property, an array of properties, or any change using`*`.
88
88
89
89
```javascript
90
-
juicr.reaction('count', ({ count },_state)=> {
91
-
return { countIsPositive: count>0 }
92
-
})
90
+
juicr.reaction('count', ({ count },_state)=> {
91
+
return { countIsPositive: count>0 }
92
+
})
93
93
```
94
94
95
95
## Asynchronous actions
96
96
Actions can return a`Promise` which resolves with the state changes. When dispatching use`.then` for triggering other actions or`.catch` for errors. Eg.
@@ -117,28 +117,28 @@ Larger projects may benefit from using multiple Juicrs for different parts of yo
117
117
## Use with React & React Native
118
118
Using juicr.js with React.js & React Native is easy. The simplest approach is to listen to all changes`*` in your main app component and use`setState` to update your state:
119
119
```javascript
120
-
// App.js
121
-
constructor() {
122
-
...
123
-
this.juicr.listen("*", (changedState,_state)=> {
124
-
this.setState({...changedState })
125
-
})
126
-
...
127
-
}
120
+
// App.js
121
+
constructor() {
122
+
...
123
+
this.juicr.listen("*", (changedState,_state)=> {
124
+
this.setState({...changedState })
125
+
})
126
+
...
127
+
}
128
128
```
129
129
Then pass the`juicr.dispatch` function to components:
130
130
```javascript
131
-
<MyComponent dispatch={this.juicr.dispatch}/>
131
+
<MyComponent dispatch={this.juicr.dispatch}/>
132
132
```
133
133
Alternatively you could pass the entire juicr to your components and let them handle their own internal state and listen for changes, e.g: