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

Commit d8f1f5a

Browse files
committed
减小app.js体积
1 parent f38300d commit d8f1f5a

File tree

5 files changed

+116
-9
lines changed

5 files changed

+116
-9
lines changed

src/containers/BookList/BookList.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* Created by Administrator on 2016/7/1.
33
*/
44
import React from 'react'
5-
//import { bindActionCreators } from 'redux'
6-
//import { connect } from 'react-redux'
75
import PropTypes from 'prop-types'
86

97
export default class BookList extends React.Component {

src/containers/Commons/Provider.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//该文件是Provider源码,从这个HOC中,你可以学习到什么?自己看吧!
2+
3+
import { Component, Children } from 'react'
4+
import PropTypes from 'prop-types'
5+
import { storeShape, subscriptionShape } from 'utils/PropTypes'
6+
7+
export function createProvider(storeKey = 'store', subKey) {
8+
const subscriptionKey = subKey || `${storeKey}Subscription`
9+
class Provider extends Component {
10+
constructor(props, context) {
11+
super(props, context)
12+
this[storeKey] = props.store;
13+
}
14+
getChildContext() {
15+
return { [storeKey]: this[storeKey], [subscriptionKey]: null }
16+
}
17+
render() {
18+
return Children.only(this.props.children)
19+
}
20+
}
21+
Provider.propTypes = {
22+
store: storeShape.isRequired,
23+
children: PropTypes.element.isRequired
24+
}
25+
Provider.childContextTypes = {
26+
[storeKey]: storeShape.isRequired,
27+
[subscriptionKey]: subscriptionShape
28+
}
29+
return Provider
30+
}
31+
export default createProvider()

src/containers/Commons/connect.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import connectAdvanced from '../components/connectAdvanced'
2+
import shallowEqual from '../utils/shallowEqual'
3+
import defaultMapDispatchToPropsFactories from './mapDispatchToProps'
4+
import defaultMapStateToPropsFactories from './mapStateToProps'
5+
import defaultMergePropsFactories from './mergeProps'
6+
import defaultSelectorFactory from './selectorFactory'
7+
8+
function match(arg, factories, name) {
9+
for (let i = factories.length - 1; i >= 0; i--) {
10+
const result = factories[i](arg)
11+
if (result) return result
12+
}
13+
return (dispatch, options) => {
14+
throw new Error(`Invalid value of type ${typeof arg} for ${name} argument when connecting component ${options.wrappedComponentName}.`)
15+
}
16+
}
17+
18+
function strictEqual(a, b) { return a === b }
19+
20+
// createConnect with default args builds the 'official' connect behavior. Calling it with
21+
// different options opens up some testing and extensibility scenarios
22+
export function createConnect({
23+
connectHOC = connectAdvanced,
24+
mapStateToPropsFactories = defaultMapStateToPropsFactories,
25+
mapDispatchToPropsFactories = defaultMapDispatchToPropsFactories,
26+
mergePropsFactories = defaultMergePropsFactories,
27+
selectorFactory = defaultSelectorFactory
28+
} = {}) {
29+
return function connect(
30+
mapStateToProps,
31+
mapDispatchToProps,
32+
mergeProps,
33+
{
34+
pure = true,
35+
areStatesEqual = strictEqual,
36+
areOwnPropsEqual = shallowEqual,
37+
areStatePropsEqual = shallowEqual,
38+
areMergedPropsEqual = shallowEqual,
39+
...extraOptions
40+
} = {}
41+
) {
42+
const initMapStateToProps = match(mapStateToProps, mapStateToPropsFactories, 'mapStateToProps')
43+
const initMapDispatchToProps = match(mapDispatchToProps, mapDispatchToPropsFactories, 'mapDispatchToProps')
44+
const initMergeProps = match(mergeProps, mergePropsFactories, 'mergeProps')
45+
return connectHOC(selectorFactory, {
46+
// used in error messages
47+
methodName: 'connect',
48+
// used to compute Connect's displayName from the wrapped component's displayName.
49+
getDisplayName: name => `Connect(${name})`,
50+
// if mapStateToProps is falsy, the Connect component doesn't subscribe to store state changes
51+
shouldHandleStateChanges: Boolean(mapStateToProps),
52+
// passed through to selectorFactory
53+
initMapStateToProps,
54+
initMapDispatchToProps,
55+
initMergeProps,
56+
pure,
57+
areStatesEqual,
58+
areOwnPropsEqual,
59+
areStatePropsEqual,
60+
areMergedPropsEqual,
61+
// any extra options args can override defaults of connect or connectAdvanced
62+
...extraOptions
63+
})
64+
}
65+
}
66+
67+
export default createConnect()

src/entry.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,16 @@ import { createStore, applyMiddleware, combineReducers } from 'redux'
44
import { Provider } from 'react-redux'
55
import { composeWithDevTools } from 'redux-devtools-extension'
66
import thunk from 'redux-thunk'
7-
import { routerReducer, routerMiddleware } from 'react-router-redux'
8-
import { AppContainer } from 'react-hot-loader'
7+
import { routerReducer } from 'react-router-redux/lib/reducer'
8+
import AppContainer from 'react-hot-loader/lib/AppContainer'
99
import App from './App'
10-
import createHistory from 'history/createBrowserHistory'
1110
import rootReducer from './reducers/index'
1211

1312
const FastClick = require('fastclick')
1413

15-
const history = createHistory()
16-
const middleware = routerMiddleware(history)
17-
1814
//解决移动端300毫秒延迟
1915
FastClick.attach(document.body)
20-
const middlewares = [thunk, middleware]
16+
const middlewares = [thunk]
2117

2218
const store = createStore(
2319
combineReducers({routing: routerReducer, ...rootReducer}),
@@ -39,6 +35,7 @@ render(App)
3935
if(module.hot) {
4036
module.hot.accept('./App', () => {
4137
const NextRootContainer = require('./App').default
38+
console.log(NextRootContainer)
4239
render(NextRootContainer)
4340
})
4441
}

src/utils/PropTypes.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import PropTypes from 'prop-types'
2+
3+
export const subscriptionShape = PropTypes.shape({
4+
trySubscribe: PropTypes.func.isRequired,
5+
tryUnsubscribe: PropTypes.func.isRequired,
6+
notifyNestedSubs: PropTypes.func.isRequired,
7+
isSubscribed: PropTypes.func.isRequired
8+
})
9+
10+
export const storeShape = PropTypes.shape({
11+
subscribe: PropTypes.func.isRequired,
12+
dispatch: PropTypes.func.isRequired,
13+
getState: PropTypes.func.isRequired
14+
})

0 commit comments

Comments
 (0)