-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnormalRedux.js
55 lines (46 loc) · 1.14 KB
/
normalRedux.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Collect dom element.
const counterEl = document.getElementById("counter");
const incrementEl = document.getElementById("increment");
const decrementEl = document.getElementById("decrement");
// Initial state.
const initialState = {
value: 0,
};
// Create reducer function.
const handleCounter = (state = initialState, action) => {
if(action.type === "increment") {
return {
...state,
value: state.value + 3,
};
} else if (action.type === "decrement") {
return {
...state,
value: state.value - 2,
};
} else {
return state
}
};
// Create redux store.
const store = Redux.createStore(handleCounter);
// Create render function.
const render = () => {
const state = store.getState();
counterEl.innerText = state.value.toString();
};
// Initially call the function.
render();
// Create subscriber.
store.subscribe(render);
// Action button.
incrementEl.addEventListener("click", () => {
store.dispatch({
type: "increment",
})
});
decrementEl.addEventListener("click", () => {
store.dispatch({
type: "decrement",
})
});