Skip to content

Commit ba3a878

Browse files
committed
couter更改
1 parent 8500956 commit ba3a878

File tree

7 files changed

+87
-66
lines changed

7 files changed

+87
-66
lines changed

.eslintignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ src/page/echartBox/index.js
55
src/page/heightEchart/index.js
66
src/page/chinaMap/index.js
77
src/page/useCallBack/index.js
8-
src/serviceWorker.js
8+
src/serviceWorker.js
9+
# src/index.js

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ module.exports = {
1111
"react/no-array-index-key": "off",
1212
"no-console": "off",
1313
"prettier/prettier": "error",
14-
"react/jsx-one-expression-per-line": "off"
14+
"react/jsx-one-expression-per-line": "off",
15+
"no-unused-vars": "off"
1516
},
1617
env: {
1718
browser: true

src/index.js

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,27 @@ import ReactDOM from "react-dom";
33
import "./index.css";
44

55
import { createStore, applyMiddleware } from "redux";
6-
// import { createLogger } from 'redux-logger';
6+
import { createLogger } from "redux-logger";
77
import { Provider } from "react-redux";
88

99
import reducer from "./page/reducer/index";
10-
import req from "./req/index";
1110
import AppRouter from "./router/index";
12-
// import thunkMiddleware from 'redux-thunk'
11+
import thunkMiddleware from "./thunkMiddleware";
1312
import * as serviceWorker from "./serviceWorker";
1413

1514
/**
1615
* 官方自带的操作显示工具
1716
*/
18-
// const loggerMiddleware = createLogger();
17+
const loggerMiddleware = createLogger();
1918

20-
const logger = store => next => action => {
21-
console.log("dispatching", action);
22-
const result = next(action);
23-
console.log("next state", store.getState());
24-
return result;
25-
};
19+
// const logger = store => next => action => {
20+
// console.log("dispatching", action);
21+
// const result = next(action);
22+
// console.log("next state", store.getState());
23+
// return result;
24+
// };
2625

27-
const thunkMiddleware = store => next => action => {
28-
console.log("拦截了");
29-
if (action.type === "changeMessageCount") {
30-
console.log("符合条件");
31-
setTimeout(() => {
32-
req.user
33-
.getMyInfo()
34-
.then(res => {
35-
if (res.code !== 0) {
36-
req.err.show(res);
37-
return;
38-
}
39-
console.log("timerMiddleware", res.data);
40-
store.dispatch({ type: "receiveCount", phone: res.data.mobile });
41-
})
42-
.catch(req.err.show);
43-
}, 2000);
44-
}
45-
next(action);
46-
};
47-
48-
const store = createStore(reducer, applyMiddleware(thunkMiddleware, logger));
26+
const store = createStore(reducer, applyMiddleware(thunkMiddleware, loggerMiddleware));
4927

5028
ReactDOM.render(
5129
<Provider store={store}>

src/page/Counter/index.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,35 @@ const mapDispatchToProps = {
1515
type: "decrease",
1616
count: 10
1717
}),
18-
changeMessageCount: () => ({
19-
type: "changeMessageCount"
18+
changeName: () => ({
19+
type: "changeName"
2020
})
2121
};
2222

23-
function Page({ value, onIncreaseClick, onDecreaseClick }) {
23+
function Page({ onIncreaseClick, onDecreaseClick, changeName }) {
2424
return (
2525
<div style={{ marginLeft: 50 }}>
26-
<h1>{value}</h1>
26+
{/* <h1>{value}</h1> */}
27+
<p>
28+
react-redux的异步流实现,是通过拦截派发给reducer的事件实现的,例如派发了一个改变全局状态中name的变化,同时循环执行其他操作
29+
</p>
2730
<button type="button" onClick={onIncreaseClick}>
2831
+
2932
</button>
3033
<button type="button" style={{ marginLeft: 50 }} onClick={onDecreaseClick}>
3134
-
3235
</button>
36+
<button type="button" style={{ marginLeft: 50 }} onClick={changeName}>
37+
异步流改变全局状态中的名字,延迟5秒
38+
</button>
3339
</div>
3440
);
3541
}
3642

3743
Page.propTypes = {
38-
value: PropTypes.number.isRequired,
3944
onIncreaseClick: PropTypes.func.isRequired,
40-
onDecreaseClick: PropTypes.func.isRequired
45+
onDecreaseClick: PropTypes.func.isRequired,
46+
changeName: PropTypes.func.isRequired
4147
};
4248

4349
export default connect(mapStateToProps, mapDispatchToProps)(Page);

src/page/reducer/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@ function caculateNum(count = 0, action) {
99
}
1010
}
1111

12-
const reducer = (state = { count: 0, phone: "", name: "Ming" }, action) => ({
12+
function caculateName(name, action) {
13+
switch (action.type) {
14+
case "setName":
15+
return action.name;
16+
default:
17+
return name;
18+
}
19+
}
20+
21+
const reducer = (state = { count: 0, name: "Ming" }, action) => ({
1322
...state,
1423
count: caculateNum(state.count, action),
15-
phone: action.phone
24+
name: caculateName(state.name, action)
1625
});
1726

1827
export default reducer;

src/router/index.js

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import React from "react";
33
import { HashRouter, Route, Link, Switch } from "react-router-dom";
44
// import { browserHistory, Router} from 'react-router';
5+
import { connect } from "react-redux";
56
import { Modal, message } from "antd";
67
import PropTypes from "prop-types";
78
import Home from "../page/CssTest/index";
@@ -17,6 +18,10 @@ import HeightEchart from "../page/heightEchart/index";
1718
import FormBox from "../page/form/index";
1819
import PromiseTest from "../page/promise/index";
1920

21+
const mapStateToProps = state => ({
22+
value: state.count,
23+
name: state.name
24+
});
2025
/**
2126
* 解决ant-design的路由跳转,modal和message组件不关闭问题
2227
*/
@@ -185,35 +190,45 @@ function QueryChildren({ match }) {
185190
);
186191
}
187192

188-
function App() {
193+
function App({ value, name }) {
189194
return (
190195
// basename 为路由前添加前缀
191196
// <BrowserRouter basename={'/douyu'}>
192-
<HashRouter>
193-
<div style={{ marginLeft: 40 }}>
194-
<Header />
195-
<Switch>
196-
<Route exact path="/" component={Home} />
197-
<Route path="/query" component={RouteQuery} />
198-
<Route path="/link" component={RouteLink} />
199-
<Route path="/params" component={RouteParams} />
200-
<Route path="/queryChildren" component={QueryChildren} />
201-
<Route path="/counter" component={Counter} />
202-
<Route path="/optimize" component={Optimize} />
203-
<Route path="/echart" component={EchartBox} />
204-
<Route path="/map" component={ChinaMap} />
205-
<Route path="/useMemo" component={UseMemo} />
206-
<Route path="/useCallback" component={UseCallback} />
207-
<Route path="/memo" component={Memo} />
208-
<Route path="/heightEchart" component={HeightEchart} />
209-
<Route path="/form" component={FormBox} />
210-
<Route path="/testPromise" component={PromiseTest} />
211-
</Switch>
197+
<>
198+
<div style={{ paddingLeft: 50, fontSize: 20 }}>
199+
<span>全局状态显示:</span>
200+
<span>{value}</span>
201+
<span style={{ paddingLeft: 50 }}>{name}</span>
212202
</div>
213-
</HashRouter>
203+
<HashRouter>
204+
<div style={{ marginLeft: 40 }}>
205+
<Header />
206+
<Switch>
207+
<Route exact path="/" component={Home} />
208+
<Route path="/query" component={RouteQuery} />
209+
<Route path="/link" component={RouteLink} />
210+
<Route path="/params" component={RouteParams} />
211+
<Route path="/queryChildren" component={QueryChildren} />
212+
<Route path="/counter" component={Counter} />
213+
<Route path="/optimize" component={Optimize} />
214+
<Route path="/echart" component={EchartBox} />
215+
<Route path="/map" component={ChinaMap} />
216+
<Route path="/useMemo" component={UseMemo} />
217+
<Route path="/useCallback" component={UseCallback} />
218+
<Route path="/memo" component={Memo} />
219+
<Route path="/heightEchart" component={HeightEchart} />
220+
<Route path="/form" component={FormBox} />
221+
<Route path="/testPromise" component={PromiseTest} />
222+
</Switch>
223+
</div>
224+
</HashRouter>
225+
</>
214226
);
215227
}
216-
228+
App.propTypes = {
229+
name: PropTypes.string.isRequired,
230+
value: PropTypes.number.isRequired
231+
};
217232
RouteParams.propTypes = {
218233
location: PropTypes.object.isRequired
219234
};
@@ -226,4 +241,4 @@ RouteQuery.propTypes = {
226241
QueryChildren.propTypes = {
227242
match: PropTypes.object.isRequired
228243
};
229-
export default App;
244+
export default connect(mapStateToProps)(App);

src/thunkMiddleware.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const thunkMiddleware = store => next => action => {
2+
console.log("拦截了");
3+
if (action.type === "changeName") {
4+
setTimeout(() => {
5+
store.dispatch({ type: "setName", name: "炎黄子孙" });
6+
}, 5000);
7+
}
8+
next(action);
9+
};
10+
11+
export default thunkMiddleware;

0 commit comments

Comments
 (0)