-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.thunk.html
75 lines (65 loc) · 1.79 KB
/
example.thunk.html
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
<!-- redux-thunk 测试-->
<!DOCTYPE html>
<html>
<head>
<title>Redux basic example</title>
<script src="./index.js"></script>
<script src="./redux-thunk.js"></script>
</head>
<body>
<div>
<p>
look this: <span id="value">o.0</span>
<button id="XXX">xxx</button>
<button id="GET">获取本仓库的详细信息</button>
</p>
</div>
<script>
function counter(state, action) {
if (typeof state === 'undefined') {
return 'o.0'
}
switch (action.type) {
case 'XXX':
return 'xxxxxx'
case 'GET':
return action.payload
default:
return state
}
}
var store = Redux.createStore(counter, Redux.applyMiddleware(thunk)) //*
var valueEl = document.getElementById('value')
function render() {
valueEl.innerHTML = store.getState().toString()
}
render()
store.subscribe(render)
document.getElementById('XXX')
.addEventListener('click', function () {
store.dispatch({
type: 'XXX'
})
})
//*
const getRepoInfo = () => {
return (dispatch) => {
fetch('https://api.github.com/repos/okkjoo/okkjoo-leetcodeHot-byJs')
.then((res) => res.json())
.then(res => res.description)
.then(desc => {
console.log(desc)
dispatch({
type: 'GET',
payload: desc
})
})
}
}
document.getElementById('GET')
.addEventListener('click', function () {
store.dispatch(getRepoInfo())
})
</script>
</body>
</html>