Hullo JS
Search…
Redux
import { Redux } from '@hullo/core'
Redux is created with a set of reducers and initial state. Reducers are put into an object where keys are action tags and properties are reducing functions. Each said function will receive a state and action payload (not whole action) and should return new state (optionally - asynchronously, with a Promise).
Redux, like Atom, has unwrap method that allows for synchronous access to stored value.
Redux::next should be provided with an object of shape:
1
{
2
action: ACTION,
3
data: ACTION_DATA
4
}
Copied!
Consider this example:
1
const redux = new Redux(
2
{
3
addNumber(state, data: number) {
4
return { state: [...state, data] };
5
},
6
async addString(state, data: string) {
7
return {
8
state: [...state, data],
9
effects: [
10
{ type: "success", data: null }
11
]
12
};
13
},
14
success(state) {
15
// this is just for demonstration sake
16
// you should try to make actions without side-effects
17
console.log("πŸŽ‰ success!");
18
return state;
19
}
20
},
21
new Array<string | number>()
22
);
23
​
24
await redux.next({ type: "addNumber", data: 0 });
25
// redux.unwrap() is [0]
26
​
27
await redux.next({ type: "addString", data: "a" });
28
// redux.unwrap() is [0, "a"]
Copied!
Last modified 1yr ago
Copy link