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
2) Add an action with a name and a function that returns the changed state:
34
34
```javascript
35
35
juicr.action("count", (amount,_state)=> {
36
-
return { count: count+= amount }
36
+
return { count:_state.count+= amount }
37
37
})
38
38
```
39
39
3) Listen to state changes. You can either listen to a single property, an array or use`*` to listen to all changes:
@@ -48,29 +48,32 @@ setInterval(() => {
48
48
juicr.dispatch("count",1)
49
49
},1000)
50
50
```
51
-
Play with this example in CodePen.
51
+
Play with this example in[CodePen](https://codepen.io/alexfoxy/pen/gyNaYw).
52
52
53
-
For use with React see #Usewith React & React Native
53
+
For use with React see #use-with-react--react-native
54
54
55
55
## API
56
-
###`newJuicr({ initialState={}, dev=false })`
56
+
####`newJuicr({ initialState={}, dev=false })`
57
57
Initializes a new Juicr. Pass in an`initialState` object and an optional`dev` flag. When dev mode is enabled all changes to the state are printed to the console.
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
62
```javascript
62
63
juicr.action('delete', ({ id },_state)=> {
63
64
return { items:_state.items.filter(t=>t.id!== id ) }
64
65
})
65
66
```
66
67
67
-
### `juicr.dispatch('actionName', data)`
68
+
69
+
#### `juicr.dispatch('actionName', data)`
68
70
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`*`.
Actions can return a`Promise` which resolves with the state changes. When dispatching use`.then` for triggering other actions or`.catch` for errors. Eg.
97
101
98
102
```javascript
@@ -136,9 +140,10 @@ Alternatively you could pass the entire juicr to your components and let them ha