Skip to content

Commit 3d0bbb8

Browse files
committed
react-redux的异步流
1 parent 45d096d commit 3d0bbb8

File tree

11 files changed

+577
-18
lines changed

11 files changed

+577
-18
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
"react-redux": "^7.1.1",
5555
"react-router": "^5.1.2",
5656
"redux": "^4.0.4",
57+
"redux-logger": "^3.0.6",
58+
"redux-thunk": "^2.3.0",
5759
"reqwest": "^2.0.5",
5860
"resolve": "1.12.0",
5961
"resolve-url-loader": "3.1.0",

src/index.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,48 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import './index.css';
44
import AppRouter from './router/index.js';
5+
// import thunkMiddleware from 'redux-thunk'
56
import * as serviceWorker from './serviceWorker';
6-
import { createStore } from 'redux';
7+
import { createStore, applyMiddleware } from 'redux';
8+
import { createLogger } from 'redux-logger'
9+
710
import reducer from './page/reducer/index';
811
import { Provider } from 'react-redux';
12+
import req from './req/index.js'
13+
14+
const loggerMiddleware = createLogger();
15+
16+
const logger = store => next => action => {
17+
console.log('dispatching', action)
18+
let result = next(action)
19+
console.log('next state', store.getState())
20+
return result
21+
}
22+
23+
const thunkMiddleware = store => next => action => {
24+
console.log('拦截了');
25+
if (action.type === 'changeMessageCount') {
26+
console.log('符合条件');
27+
setTimeout(() => {
28+
req.user.getMyInfo()
29+
.then((res) => {
30+
if (res.code !== 0) {
31+
req.err.show(res);
32+
return;
33+
}
34+
console.log('timerMiddleware', res.data);
35+
store.dispatch({ type: 'receiveCount', phone: res.data.mobile });
36+
})
37+
.catch(req.err.show)
38+
}, 2000);
39+
}
40+
next(action);
41+
};
942

10-
const store = createStore(reducer);
43+
const store = createStore(
44+
reducer,
45+
applyMiddleware(thunkMiddleware, logger)
46+
);
1147

1248
ReactDOM.render(
1349
<Provider store={store}>

src/page/Counter/index.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import { connect } from 'react-redux';
22
import React from 'react';
33

44
const mapStateToProps = (state) => {
5-
console.log('state对象', state);
5+
// console.log('state对象', state);
66
return {
7-
value: state.count
7+
value: state.count,
8+
phone: state.phone,
89
}
910
}
1011

@@ -21,14 +22,21 @@ const mapDispatchToProps = {
2122
count: 10,
2223
}
2324
},
25+
changeMessageCount: () => {
26+
return {
27+
type: 'changeMessageCount',
28+
}
29+
},
2430
}
2531

26-
function Page({ value, onIncreaseClick, onDecreaseClick }) {
32+
function Page({ value, phone, onIncreaseClick, onDecreaseClick, changeMessageCount }) {
2733
return (
2834
<div style={{ marginLeft: 50 }}>
2935
<h1>{value}</h1>
3036
<button onClick={onIncreaseClick}>+</button>
3137
<button style={{ marginLeft: 50 }} onClick={onDecreaseClick}>-</button>
38+
<button style={{ marginLeft: 50 }} onClick={changeMessageCount}>redux异步</button>
39+
<h2 style={{ marginTop: 20 }}>{phone}</h2>
3240
</div>
3341
)
3442
}

src/page/promise/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ import { useEffect, useState } from 'react';
33
import req from '../../req';
44

55
function Page() {
6-
const [infos, setInfos] = useState(null);
6+
// const [infos, setInfos] = useState(null);
77
function getCrtUserInfos() {
8-
req.user.getMyAuthority()
8+
req.user.getMyInfo()
99
.then((res) => {
1010
if (res.code !== 0) {
1111
// req.err.show(res);
1212
return;
1313
}
14-
setInfos(res.data);
15-
console.log('getCrtUserInfos', infos);
14+
console.log('getCrtUserInfos');
1615
})
1716
// .catch(req.err.show);
1817
}

src/page/reducer/index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
2-
const reducer = (state = { count: 0, name: 'Ming' }, action) => {
1+
import req from '../../req';
2+
const reducer = (state = { count: 0, phone: '', name: 'Ming' }, action) => {
33
return {
44
...state,
55
count: caculateNum(state.count, action),
6+
phone: action.phone,
67
}
78
};
89

@@ -16,4 +17,14 @@ function caculateNum( count = 0, action) {
1617
return count;
1718
}
1819
}
20+
function caculatePhone( phone = '', action) {
21+
switch (action.type) {
22+
case 'changeMessageCount':
23+
case 'receiveCount':
24+
return action.phone;
25+
default:
26+
return phone;
27+
}
28+
}
29+
1930
export default reducer;

src/req/api/transform.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import reqwest from 'reqwest';
22
import { baseUrl } from '../config';
3-
import session from './session';
3+
// import session from './session';
44

55
function wrapper(api) {
66
console.log('api', api);
77
return (data, originData = null) => {
8-
const sessionId = session.get();
8+
const sessionId = '114f273d90b04a5fbcd1df677ea6d010';
99
const {
1010
method, headers = {},
1111
} = api;
1212
if (sessionId) {
13-
headers.sessionId = '38d30a51aedb4e7d94f46df8c0cf4654';
13+
headers.sessionId = sessionId;
1414
}
1515
let newData = null;
1616
let { url } = api;

src/req/api/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const api = {
22
getMyInfo: {
3-
url: '/eobi-api/api/sys/currentUserInfo',
3+
url: '/bi-api/api/sys/currentUserInfo',
44
},
55
};
66

src/req/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import req from './req';
22
import api from './api/index';
3-
// import err from './fn/err';
3+
import err from './fn/err';
44
import cachify from './fn/cachify';
55
import authority from './fn/authority';
66

77
req.use(api);
8-
// req.use(err);
8+
req.use(err);
99
req.use(cachify);
1010
req.use(authority);
1111

src/router/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function Header() {
4949
<Link to="/queryChildren">嵌套路由</Link>
5050
</li>
5151
<li>
52-
<Link to="/counter">全局状态共享计数器操作</Link>
52+
<Link to="/counter">react-redux的应用和异步流</Link>
5353
</li>
5454
<li>
5555
<Link to="/single">计数器显示</Link>

0 commit comments

Comments
 (0)