Skip to content
This repository was archived by the owner on May 7, 2022. It is now read-only.

Commit 7690da4

Browse files
author
yongyue.huang
committed
add hooks
1 parent 23eee2b commit 7690da4

File tree

8 files changed

+2167
-1345
lines changed

8 files changed

+2167
-1345
lines changed

.eslintrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"parser": "babel-eslint",
3+
"plugins": [
4+
"react-hooks"
5+
],
6+
"rules": {
7+
"react-hooks/rules-of-hooks": "error",
8+
"react-hooks/exhaustive-deps": "warn"
9+
}
10+
}

package-lock.json

Lines changed: 1560 additions & 766 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"serve": "serve -s build"
99
},
1010
"dependencies": {
11+
"acorn-dynamic-import": "^4.0.0",
1112
"antd": "^3.10.9",
1213
"axios": "^0.18.0",
1314
"chalk": "2.4.1",
@@ -26,9 +27,9 @@
2627
"progress-bar-webpack-plugin": "^1.11.0",
2728
"promise": "8.0.1",
2829
"raf": "3.4.1",
29-
"react": "16.6.3",
30+
"react": "^16.8.6",
3031
"react-dev-utils": "^5.0.1",
31-
"react-dom": "16.6.3",
32+
"react-dom": "^16.8.6",
3233
"react-fastclick": "^3.0.2",
3334
"react-infinite-scroller": "^1.2.2",
3435
"react-loadable": "^5.5.0",
@@ -39,6 +40,7 @@
3940
"redux-devtools-extension": "^2.13.7",
4041
"redux-immutable": "^4.0.0",
4142
"redux-localstorage": "^0.4.1",
43+
"redux-react-hook": "^3.3.1",
4244
"redux-thunk": "2.3.0",
4345
"resolve": "1.8.1",
4446
"serve": "10.1.1",
@@ -58,6 +60,7 @@
5860
"@babel/preset-env": "^7.1.6",
5961
"@babel/preset-react": "^7.0.0",
6062
"@babel/runtime": "^7.1.5",
63+
"acorn": "^6.0.5",
6164
"autoprefixer": "9.3.1",
6265
"babel-eslint": "^9.0.0",
6366
"babel-loader": "^8.0.4",
@@ -71,6 +74,7 @@
7174
"eslint-plugin-import": "2.14.0",
7275
"eslint-plugin-jsx-a11y": "5.0.1",
7376
"eslint-plugin-react": "7.11.1",
77+
"eslint-plugin-react-hooks": "^1.6.0",
7478
"file-loader": "2.0.0",
7579
"less": "^3.8.1",
7680
"less-loader": "^4.1.0",
@@ -79,8 +83,8 @@
7983
"postcss-pxtorem": "^4.0.1",
8084
"style-loader": "0.23.1",
8185
"url-loader": "1.1.2",
82-
"webpack": "4.26.1",
83-
"webpack-dev-server": "3.1.10",
86+
"webpack": "^4.28.4",
87+
"webpack-dev-server": "^3.3.1",
8488
"webpack-manifest-plugin": "2.0.4"
8589
}
8690
}

src/App.js

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,57 @@
1-
import React from 'react'
1+
import React, { useEffect, useContext, useCallback } from 'react'
2+
import { useDispatch, useMappedState, StoreContext } from 'redux-react-hook'
23
import { Route, BrowserRouter as Router } from 'react-router-dom'
34
import routes from 'routes'
4-
import { bindActionCreators } from 'redux'
5-
import { connect } from 'react-redux'
65
import { setGlobalLoading } from 'actions/global'
76
import Loading from 'components/Loading/Loading'
87

9-
@connect(
10-
state => state.getIn(['global']),
11-
dispatch => bindActionCreators({ setGlobalLoading }, dispatch)
12-
)
13-
class App extends React.Component {
14-
componentDidMount () {
15-
this.hiddenLoading()
16-
}
17-
componentDidUpdate () {
18-
this.hiddenLoading()
19-
}
20-
hiddenLoading = () => {
21-
const { globalLoading } = this.props
8+
export default function App() {
9+
/*
10+
* @description 从redux取值——方案1
11+
* */
12+
const store = useContext(StoreContext)
13+
const { globalLoading } = store.getState().toJS().global
14+
const dispatch = useDispatch()
15+
16+
/*
17+
* @description 从redux取值——方案2
18+
* */
19+
// const mapState = useCallback(
20+
// state => ({
21+
// global: state.toJS().global
22+
// }),
23+
// [],
24+
// );
25+
// const { global } = useMappedState(mapState);
26+
27+
const hiddenLoading = () => {
2228
if (globalLoading) {
2329
setTimeout(() => {
24-
this.props.setGlobalLoading(false)
30+
dispatch(setGlobalLoading(false))
2531
}, 0)
2632
}
2733
}
28-
render () {
29-
const { globalLoading } = this.props
30-
return (
31-
<Router>
32-
<div>
33-
{globalLoading && <Loading />}
34-
{
35-
routes.map((r, key) => {
36-
return (
37-
<Route
38-
render={props => (<r.component {...props} routes={r.routes} />)}
39-
exact={!!r.exact}
40-
key={r.path + key}
41-
path={r.path}
42-
/>
43-
)
44-
})
45-
}
46-
</div>
47-
</Router>
48-
)
49-
}
34+
35+
useEffect(() => {
36+
hiddenLoading()
37+
}, [])
38+
return (
39+
<Router>
40+
<div>
41+
{globalLoading && <Loading/>}
42+
{
43+
routes.map((r, key) => {
44+
return (
45+
<Route
46+
render={props => (<r.component {...props} routes={r.routes}/>)}
47+
exact={!!r.exact}
48+
key={r.path + key}
49+
path={r.path}
50+
/>
51+
)
52+
})
53+
}
54+
</div>
55+
</Router>
56+
)
5057
}
51-
export default App

0 commit comments

Comments
 (0)