Skip to content

Commit bd53efe

Browse files
author
yunkui.zhou
committed
提取store公用部分
1 parent 6024628 commit bd53efe

File tree

18 files changed

+110
-86
lines changed

18 files changed

+110
-86
lines changed
+7-20
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,18 @@
11
/**
22
* Created by ink on 2018/4/8.
33
*/
4-
import {connect} from 'react-redux'
5-
import {bindActionCreators} from 'redux'
64
import * as actions from '../redux/action'
75
import React, {Component} from 'react'
6+
import connect from '../../tools/connect'
87

98
class Center extends Component {
109
render () {
1110
return (<div>欢迎来到个人中心</div>)
1211
}
1312
}
14-
const mapStateToProps = state => {
15-
return {
16-
title: state.center.title,
17-
router: state.router
18-
}
19-
}
20-
/**
21-
* 如果不显示的设置返回dispatch,那么原先在组件内可通过this.props访问的dispatch将消失
22-
* @param dispatch
23-
* @returns {{actions: (A|B|M|N)}}
24-
*/
25-
const mapDispatchToProps = dispatch => {
26-
return {
27-
actions: bindActionCreators(actions, dispatch),
28-
//dispatch
29-
}
30-
}
31-
export default connect(mapStateToProps, mapDispatchToProps)(Center)
13+
export default connect({
14+
title: state => state.center.title,
15+
router: state => state.router
16+
}, {
17+
actions
18+
})(Center)

client/pages/integration/Home/index.js

+7-21
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
* Created by ink on 2018/4/8.
33
*/
44
import React, {Component} from 'react'
5-
import {connect} from 'react-redux'
6-
import {bindActionCreators} from 'redux'
5+
import connect from '../../tools/connect'
76
import * as actions from '../redux/action'
87
class Home extends Component {
98
constructor(options) {
109
super(options)
11-
console.log(this.props.title)
1210
}
1311
componentDidMount() {
1412
setTimeout(() => {
@@ -23,21 +21,9 @@ class Home extends Component {
2321
</div>)
2422
}
2523
}
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)
24+
export default connect({
25+
title: state => state.home.title,
26+
router: state => state.router
27+
}, {
28+
actions
29+
})(Home)

client/pages/integration/container/Root.local.js client/pages/integration/Root/Local.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44

55
import React, {Fragment} from 'react'
6-
import DevTools from '../DevTools/index'
6+
import DevTools from '../../tools/DevTools/index'
77
const Content = (props) => {
88
const {component: CustomerContent} = props
99
return (<Fragment>

client/pages/integration/container/Root.js client/pages/integration/Root/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import {Provider} from 'react-redux'
66
import React from 'react'
77
let ContentEnv
88
if (process.env.NODE_ENV === 'local') {
9-
ContentEnv = require('./Root.local')
9+
ContentEnv = require('./Local')
1010
} else {
11-
ContentEnv = require('./Root.prod')
11+
ContentEnv = require('./Prod')
1212
}
1313
const RealContent = ContentEnv.default
1414
const Root = (props) => {

client/pages/integration/index.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
*/
44
import React from 'react'
55
import ReactDom from 'react-dom'
6-
import Root from './container/Root'
6+
import Root from './Root/index'
77
import App from './App'
8-
import store, {history} from './store/forBrowser'
8+
import storeFactory, {history} from '../tools/store/forBrowser'
9+
import reducers from './store/'
10+
const store = storeFactory(reducers)
911
ReactDom.hydrate(
1012
<Root store={store} history={history} component={App} />,
1113
document.getElementById('bd')

client/pages/integration/store/common.js

-16
This file was deleted.

client/pages/integration/store/forServer.js

-12
This file was deleted.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Created by ink on 2018/4/10.
3+
*/
4+
import {center, home} from '../../integration/redux/reducer'
5+
import {routerReducer} from 'react-router-redux'
6+
7+
const reducers = {
8+
center,
9+
home,
10+
router: routerReducer
11+
}
12+
export default reducers

client/pages/integration/DevTools/index.js client/pages/tools/DevTools/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const DevTools = createDevTools(
1515
<DockMonitor
1616
toggleVisibilityKey='ctrl-h'
1717
changePositionKey='ctrl-q'
18+
changeMonitorKey='ctrl-m'
19+
defaultPosition="top"
1820
defaultIsVisible={false}>
1921
<LogMonitor theme='tomorrow' />
2022
</DockMonitor>

client/pages/tools/connect/index.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Created by ink on 2018/3/6.
3+
*/
4+
import {bindActionCreators} from 'redux'
5+
import {connect} from 'react-redux'
6+
//PipeConnect({
7+
// someProps: (state) => {
8+
// return state.get('someProps')
9+
// }
10+
//}, {
11+
// Actions: action,
12+
// AppAction: appAction
13+
//})(component)
14+
const pipeConnect = (props, actions) => {
15+
const mapStateToProps = state => {
16+
const obj = {}
17+
Object.keys(props).forEach(key => {
18+
const v = props[key]
19+
if (typeof v !== 'function') {
20+
obj[key] = v
21+
} else {
22+
obj[key] = v(state)
23+
}
24+
})
25+
return obj
26+
}
27+
const mapDispatchToProps = (dispatch) => {
28+
const obj = {}
29+
Object.keys(actions).forEach(key => {
30+
obj[key] = bindActionCreators(actions[key], dispatch)
31+
})
32+
return obj
33+
}
34+
return (component) => {
35+
return connect(mapStateToProps, mapDispatchToProps)(component)
36+
}
37+
}
38+
export default pipeConnect

client/pages/tools/store/common.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Created by ink on 2018/4/10.
3+
*/
4+
import thunkMiddleware from 'redux-thunk'
5+
6+
const middleware = [thunkMiddleware]
7+
8+
export {middleware}
9+
export default {middleware}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Created by ink on 2018/4/10.
33
*/
4-
import {reducers, middleware} from './common'
4+
import {middleware} from './common'
55
import {routerMiddleware} from 'react-router-redux'
66
import {applyMiddleware, combineReducers} from 'redux'
77
import {createBrowserHistory} from 'history'
@@ -12,10 +12,12 @@ const history = createBrowserHistory({
1212

1313
const middlewares = middleware.concat(routerMiddleware(history))
1414

15-
const store = configureStore(
16-
combineReducers(reducers),
17-
{},
18-
applyMiddleware(...middlewares)
19-
)
15+
const store = reducers => {
16+
return configureStore(
17+
combineReducers(reducers),
18+
{},
19+
applyMiddleware(...middlewares)
20+
)
21+
}
2022
export {history}
2123
export default store

client/pages/tools/store/forServer.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Created by ink on 2018/4/10.
3+
*/
4+
import {middleware} from './common'
5+
import {applyMiddleware, combineReducers} from 'redux'
6+
import configureStore from '../configure/store'
7+
const store = reducers => {
8+
return configureStore(
9+
combineReducers(reducers),
10+
{},
11+
applyMiddleware(...middleware)
12+
)
13+
}
14+
export default store

server/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import path from 'path'
77
import React from 'react'
88
import {renderToString} from 'react-dom/server'
99

10-
import App from '../client/pages/example/App'
10+
import Component from '../client/pages/example/App'
1111
import extractMapping from './middleware/extractMapping'
1212
import mapAssets from './utils/mapAssets'
1313
import {StaticRouter} from 'react-router'
1414

1515
import {Provider} from 'react-redux'
16-
import store from '../client/pages/integration/store/forServer'
17-
import {Content} from '../client/pages/integration/container/Root'
16+
import store from '../client/pages/tools/store/forServer'
17+
import {Content} from '../client/pages/integration/Root/index'
1818
import Integration from '../client/pages/integration/App'
19-
19+
import reducers from '../client/pages/integration/store/'
2020
const app = express()
2121
app.set('views', path.join(__dirname, 'views'));
2222
app.set('view engine', 'hbs')
@@ -46,7 +46,7 @@ app.use('/components', extractMapping, (req, res, next) => {
4646
location={req.url}
4747
context={{}}
4848
basename="/components">
49-
<App/>
49+
<Component/>
5050
</StaticRouter>)
5151
res.render('components', {
5252
app: content,
@@ -56,7 +56,7 @@ app.use('/components', extractMapping, (req, res, next) => {
5656
})
5757

5858
app.use('/integration', extractMapping, (req, res, next) => {
59-
const content = renderToString(<Provider store={store}>
59+
const content = renderToString(<Provider store={store(reducers)}>
6060
<StaticRouter
6161
location={req.url}
6262
context={{}}

0 commit comments

Comments
 (0)