Repository navigation
Expand file tree
/
Copy pathbasicReduxFunctions.html
More file actions
120 lines (105 loc) · 3.02 KB
/
Copy pathbasicReduxFunctions.html
File metadata and controls
120 lines (105 loc) · 3.02 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="redux.js"></script>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
}
label {
display: inline-block;
width: 100px;
padding: 5px;
}
#result {
padding: 5px;
}
</style>
</head>
<body>
<div>
<label for="firstName">First Name:</label>
<input type="text" name="firstName" id="firstName">
<button onclick="handleFirstNameUpdate()">Update</button>
</div>
<div>
<label for="lastName">Last Name:</label>
<input type="text" name="lastName" id="lastName">
<button onclick="handleLastNameUpdate()">Update</button>
</div>
<div>
<label for="birthday">Birthday:</label>
<input type="text" name="birthday" id="birthday">
<button onclick="handleBirthdayUpdate()">Update</button>
</div>
<div id="result">
</div>
<script type="text/javascript">
// An initial state is to be passed to the Reducer function
const initState = {
firstName: 'Conway',
lastName: 'Liu',
birthday: '10/1/1970'
}
// A Reducer function is a pure function that takes in the previous state,
// the action to be dispatched, and return a new state for the store to keep
const myReducer = (state, action) => {
if (!state) state = initState;
switch (action.type) {
case 'CHANGE_FIRST_NAME':
return {
...state,
firstName: action.data
}
case 'CHANGE_LAST_NAME':
return {
...state,
lastName: action.data
}
case 'CHANGE_BIRTHDAY':
return {
...state,
birthday: action.data
}
default:
return state;
}
}
const store = Redux.createStore(myReducer);
console.log("Store State", store.getState())
const handleFirstNameUpdate = () => {
store.dispatch({
type: 'CHANGE_FIRST_NAME',
data: document.getElementById('firstName').value
})
}
const handleLastNameUpdate = () => {
store.dispatch({
type: 'CHANGE_LAST_NAME',
data: document.getElementById('lastName').value
})
}
const handleBirthdayUpdate = () => {
store.dispatch({
type: 'CHANGE_BIRTHDAY',
data: document.getElementById('birthday').value
})
}
</script>
<script>
{
const { firstName, lastName, birthday } = store.getState();
document.getElementById('result').innerText = `${firstName} ${lastName} was born on ${birthday}`
// Subscript to the state change event so we can update the UI whenever the state is changed
store.subscribe(() => {
const { firstName, lastName, birthday } = store.getState();
console.log(store.getState())
document.getElementById('result').innerText = `${firstName} ${lastName} was born on ${birthday}`
})
}
</script>
</body>
</html>