Skip to content

Commit c9d60ac

Browse files
author
yunkui.zhou
committed
添加redux
1 parent ec41bdb commit c9d60ac

File tree

13 files changed

+281
-44
lines changed

13 files changed

+281
-44
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ module.exports = {
165165
"semi": [1, "never"],
166166
"semi-spacing": ["error", { "before": false, "after": true }],
167167
"space-before-blocks": ["error", "always"],
168-
"space-before-function-paren": ["error", "always"],
168+
"space-before-function-paren": [1, "always"],
169169
"space-in-parens": ["error", "never"],
170170
"space-infix-ops": "error",
171171
"space-unary-ops": ["error", { "words": true, "nonwords": false }],

client/pages/example/App.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,53 +11,53 @@ import PullRefresh from './PullRefresh'
1111
import Slide from './Slide'
1212
import Staff from './Staff'
1313
import UploadMore from './UploadMore'
14+
const routeConfig = [{
15+
path: '/dashboard',
16+
component: Dashboard,
17+
title: '仪表盘'
18+
}, {
19+
path: '/imgupload',
20+
component: ImgUpload,
21+
title: '图片上传'
22+
}, {
23+
path: '/itemmove',
24+
component: ItemMove,
25+
title: '列表项可滑动'
26+
}, {
27+
path: '/pullrefresh',
28+
component: PullRefresh,
29+
title: '下拉刷新'
30+
}, {
31+
path: '/slide',
32+
component: Slide,
33+
title: '幻灯片'
34+
}, {
35+
path: '/staff',
36+
component: Staff,
37+
title: '画布标尺'
38+
}, {
39+
path: '/uploadmore',
40+
component: UploadMore,
41+
title: '上拉加载更多'
42+
}]
43+
const liCollection = []
44+
const routes = []
45+
for (let i = 0; i < routeConfig.length; i++) {
46+
const item = routeConfig[i]
47+
liCollection.push(<li key={item.path} className={`top-component flag${item.path.replace('/', '-')}`}>
48+
<Link to={item.path}>
49+
{item.title}
50+
</Link>
51+
</li>)
52+
routes.push(<Route key={item.path} path={item.path} component={item.component}/>)
53+
}
1454
const App = (props) => {
1555
return (<div className="wrap">
1656
<ul className="sider">
17-
<li className="top-component flag-dashboard">
18-
<Link to="/dashboard">
19-
仪表盘
20-
</Link>
21-
</li>
22-
<li className="top-component flag-imgupload">
23-
<Link to="/imgupload">
24-
图片上传
25-
</Link>
26-
</li>
27-
<li className="top-component flag-itemmove">
28-
<Link to="/itemmove">
29-
列表项可滑动
30-
</Link>
31-
</li>
32-
<li className="top-component flag-pullrefresh">
33-
<Link to="/pullrefresh">
34-
下拉刷新
35-
</Link>
36-
</li>
37-
<li className="top-component flag-slide">
38-
<Link to="/slide">
39-
幻灯片
40-
</Link>
41-
</li>
42-
<li className="top-component flag-staff">
43-
<Link to="/staff">
44-
画布标尺
45-
</Link>
46-
</li>
47-
<li className="top-component flag-uploadmore">
48-
<Link to="/uploadmore">
49-
上拉加载更多
50-
</Link>
51-
</li>
57+
{liCollection}
5258
</ul>
5359
<div className="content">
54-
<Route path="/dashboard" component={Dashboard}/>
55-
<Route path="/imgupload" component={ImgUpload}/>
56-
<Route path="/itemmove" component={ItemMove}/>
57-
<Route path="/pullrefresh" component={PullRefresh}/>
58-
<Route path="/slide" component={Slide}/>
59-
<Route path="/staff" component={Staff}/>
60-
<Route path="/uploadmore" component={UploadMore}/>
60+
{routes}
6161
</div>
6262
</div>)
6363
}

client/pages/integration/App.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Created by ink on 2018/4/8.
3+
*/
4+
import React, {Fragment} from 'react'
5+
import Center from './Center'
6+
import Home from './Home'
7+
import {Switch, Route, Link} from 'react-router-dom'
8+
9+
const App = (props) => {
10+
return (
11+
<Fragment>
12+
<div>
13+
<Link to='/home'>首页</Link>
14+
</div>
15+
<div>
16+
<Link to='/center'>用户中心</Link>
17+
</div>
18+
<Switch>
19+
<Route path='/home' component={Home}></Route>
20+
<Route path='/center' component={Center}></Route>
21+
</Switch>
22+
</Fragment>
23+
)
24+
}
25+
export default App
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Created by ink on 2018/4/8.
3+
*/
4+
import {connect} from 'react-redux'
5+
import {bindActionCreators} from 'redux'
6+
import * as actions from '../redux/action'
7+
import React, {Component} from 'react'
8+
9+
class Center extends Component {
10+
render () {
11+
console.log(this.props)
12+
return (<div>欢迎来到个人中心</div>)
13+
}
14+
}
15+
const mapStateToProps = state => {
16+
return {
17+
title: state.center.title,
18+
router: state.router
19+
}
20+
}
21+
/**
22+
* 如果不显示的设置返回dispatch,那么原先在组件内可通过this.props访问的dispatch将消失
23+
* @param dispatch
24+
* @returns {{actions: (A|B|M|N)}}
25+
*/
26+
const mapDispatchToProps = dispatch => {
27+
return {
28+
actions: bindActionCreators(actions, dispatch),
29+
//dispatch
30+
}
31+
}
32+
export default connect(mapStateToProps, mapDispatchToProps)(Center)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Created by ink on 2018/4/8.
3+
*/
4+
import React, {Component} from 'react'
5+
import {connect} from 'react-redux'
6+
import {bindActionCreators} from 'redux'
7+
import * as actions from '../redux/action'
8+
class Home extends Component {
9+
constructor(options) {
10+
super(options)
11+
console.log(this.props)
12+
}
13+
componentDidMount() {
14+
setTimeout(() => {
15+
//this.props.actions.home('我是首页')
16+
//通过action出发路由跳转
17+
this.props.actions.jump('center')
18+
}, 2000)
19+
}
20+
render() {
21+
return (<div>
22+
{this.props.title}
23+
</div>)
24+
}
25+
}
26+
const mapStateToProps = state => {
27+
return {
28+
title: state.home.title,
29+
router: state.router
30+
}
31+
}
32+
/**
33+
* 如果不显示的设置返回dispatch,那么原先在组件内可通过this.props访问的dispatch将消失
34+
* @param dispatch
35+
* @returns {{actions: (A|B|M|N)}}
36+
*/
37+
const mapDispatchToProps = dispatch => {
38+
return {
39+
actions: bindActionCreators(actions, dispatch),
40+
//dispatch
41+
}
42+
}
43+
export default connect(mapStateToProps, mapDispatchToProps)(Home)

client/pages/integration/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Created by ink on 2018/4/8.
3+
*/
4+
import React from 'react'
5+
import ReactDom from 'react-dom'
6+
import {BrowserRouter, Router} from 'react-router-dom'
7+
import App from './App'
8+
import {createStore, applyMiddleware, combineReducers} from 'redux'
9+
import {Provider} from 'react-redux'
10+
import {createBrowserHistory} from 'history'
11+
import {routerReducer, routerMiddleware, push} from 'react-router-redux'
12+
import {center, home} from './redux/reducer'
13+
const history = createBrowserHistory({
14+
basename: '/integration'
15+
})
16+
const middleware = routerMiddleware(history)
17+
18+
const store = createStore(combineReducers({
19+
center,
20+
home,
21+
router: routerReducer
22+
}), applyMiddleware(middleware)
23+
)
24+
// Now you can dispatch navigation actions from anywhere!
25+
// store.dispatch(push('/foo'))
26+
ReactDom.render(
27+
<Provider store={store}>
28+
<Router history={ history }>
29+
<App/>
30+
</Router>
31+
</Provider>
32+
,
33+
document.getElementById('bd')
34+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Created by ink on 2018/4/8.
3+
*/
4+
import {push} from 'react-router-redux'
5+
export function home (data = '') {
6+
return {
7+
type: 'home_title',
8+
data
9+
}
10+
}
11+
export function center(data = '') {
12+
return {
13+
type: 'center_title',
14+
data
15+
}
16+
}
17+
export function jump(path = '') {
18+
return push(path)
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Created by ink on 2018/4/8.
3+
*/
4+
export function center (state = {title: '欢迎来到个人中心'}, action) {
5+
switch (action.type) {
6+
case 'center_title':
7+
return Object.assign({}, state, {title: action.data})
8+
default:
9+
return state
10+
}
11+
}
12+
13+
export function home (state = {title: '默认首页'}, action) {
14+
switch (action.type) {
15+
case 'home_title':
16+
return Object.assign({}, state, {title: action.data})
17+
default:
18+
return state
19+
}
20+
}

package-lock.json

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

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,16 @@
3131
"express": "^4.16.3",
3232
"extract-text-webpack-plugin": "^4.0.0-beta.0",
3333
"hbs": "^4.0.1",
34+
"history": "^4.7.2",
3435
"less": "^3.0.1",
3536
"less-loader": "^4.1.0",
3637
"react": "^16.2.0",
3738
"react-dom": "^16.2.0",
39+
"react-redux": "^5.0.7",
3840
"react-router": "^4.2.0",
3941
"react-router-dom": "^4.2.2",
42+
"react-router-redux": "^4.0.8",
43+
"redux": "^3.7.2",
4044
"style-loader": "^0.20.3",
4145
"url-loader": "^1.0.1",
4246
"webpack": "^4.3.0",

0 commit comments

Comments
 (0)