-
Notifications
You must be signed in to change notification settings - Fork 9
/
connect.js
74 lines (63 loc) · 2.17 KB
/
connect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, { PureComponent } from 'react'
import Context from './Context'
import shallowEqual from './shallow-equal'
// Creates new props by calling `mapStateToProps` with the new `state` object
// if the state has not changed, or the new props are shallow equal with the old
// props then the selector returns the previous props
//
// Redux has a selector with the same name that uses `mapStateToProps`,
// `mapDispatchToProps`, `mergeProps`, and the Connect components own props to
// calculate the derivedProps
//
function makeDerivedPropsSelector(mapStateToProps) {
let lastState
let lastDerivedProps
return function selectDerivedProps(state) {
if (lastState === state) {
return lastDerivedProps
}
lastState = state
const nextProps = mapStateToProps(state)
const propsChanged = !shallowEqual(lastDerivedProps, nextProps)
if (propsChanged) {
lastDerivedProps = nextProps
}
return lastDerivedProps
}
}
// Returns the same child element if the derived props object has not changed
// since the last update. This avoids unnecessary re-renders of the
// WrappedComponent
function makeChildElementSelector(WrappedComponent) {
let lastChildProps
let lastChildElement
return function selectChildElement(childProps) {
if (childProps !== lastChildProps) {
lastChildProps = childProps
lastChildElement = <WrappedComponent {...childProps} />
}
return lastChildElement
}
}
export default function connect(mapStateToProps) {
return function wrapWithConnect(WrappedComponent) {
class Connect extends PureComponent {
constructor(props) {
super(props)
this.selectDerivedProps = makeDerivedPropsSelector(mapStateToProps)
this.selectChildElement = makeChildElementSelector(WrappedComponent)
this.renderWrappedComponent = this.renderWrappedComponent.bind(this)
}
renderWrappedComponent({ storeState }) {
let derivedProps = this.selectDerivedProps(storeState)
return this.selectChildElement(derivedProps)
}
render() {
return (
<Context.Consumer>{this.renderWrappedComponent}</Context.Consumer>
)
}
}
return Connect
}
}