Skip to content

Commit b648993

Browse files
committed
done
1 parent fb966e7 commit b648993

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed
File renamed without changes.

index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ <h1 class="max-w-md mx-auto text-center text-2xl font-bold">
4040
</div>
4141
</div>
4242

43-
<script src="./script.js"></script>
43+
<script src="./normalRedux.js"></script>
4444
</body>
4545
</html>

normalRedux.js

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Collect dom element.
2+
const counterEl = document.getElementById("counter");
3+
const incrementEl = document.getElementById("increment");
4+
const decrementEl = document.getElementById("decrement");
5+
6+
// Initial state.
7+
const initialState = {
8+
value: 0,
9+
};
10+
11+
// Action identifiyers.
12+
const INCREMENT = "increment";
13+
const DECREMENT = "decrement";
14+
15+
// Action crators.
16+
const increment = (value) => {
17+
return {
18+
type: INCREMENT,
19+
payload: value
20+
}
21+
};
22+
23+
const decrement = (value) => {
24+
return {
25+
type: DECREMENT,
26+
payload: value
27+
}
28+
};
29+
30+
// Create reducer function.
31+
const handleCounter = (state = initialState, action) => {
32+
if(action.type === "increment") {
33+
return {
34+
...state,
35+
value: state.value + action.payload,
36+
};
37+
} else if (action.type === "decrement") {
38+
return {
39+
...state,
40+
value: state.value - action.payload,
41+
};
42+
} else {
43+
return state
44+
}
45+
};
46+
47+
// Create redux store.
48+
const store = Redux.createStore(handleCounter);
49+
50+
// Create render function.
51+
const render = () => {
52+
const state = store.getState();
53+
counterEl.innerText = state.value.toString();
54+
};
55+
56+
// Initially call the function.
57+
render();
58+
59+
// Create subscriber.
60+
store.subscribe(render);
61+
62+
// Action button.
63+
incrementEl.addEventListener("click", () => {
64+
store.dispatch(increment(5))
65+
});
66+
67+
decrementEl.addEventListener("click", () => {
68+
store.dispatch(decrement(3))
69+
});

0 commit comments

Comments
 (0)