File tree 3 files changed +70
-1
lines changed
3 files changed +70
-1
lines changed File renamed without changes.
Original file line number Diff line number Diff line change @@ -40,6 +40,6 @@ <h1 class="max-w-md mx-auto text-center text-2xl font-bold">
40
40
</ div >
41
41
</ div >
42
42
43
- < script src ="./script .js "> </ script >
43
+ < script src ="./normalRedux .js "> </ script >
44
44
</ body >
45
45
</ html >
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments