Skip to content

Commit 05ef521

Browse files
committed
Avoid useless render calls
1 parent a072c66 commit 05ef521

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/components/createConnect.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ export default function createConnect(React) {
2323
const { Component, PropTypes } = React;
2424
const storeShape = createStoreShape(PropTypes);
2525

26+
// Avoids rendering if the props did not change
27+
class PureWrap extends Component {
28+
shouldComponentUpdate(nextProps) {
29+
return !shallowEqual(this.props.passProps, nextProps.passProps);
30+
}
31+
render() {
32+
const PureWrappedComponent = this.props.component;
33+
return <PureWrappedComponent ref='wrappedInstance' {...this.props.passProps} />;
34+
}
35+
}
36+
37+
2638
return function connect(mapStateToProps, mapDispatchToProps, mergeProps, options = {}) {
2739
const shouldSubscribe = Boolean(mapStateToProps);
2840
const finalMapStateToProps = mapStateToProps || defaultMapStateToProps;
@@ -161,7 +173,11 @@ export default function createConnect(React) {
161173
}
162174

163175
getWrappedInstance() {
164-
return this.refs.wrappedInstance;
176+
if (pure) {
177+
return this.refs.wrappedInstance.refs.wrappedInstance;
178+
} else {
179+
return this.refs.wrappedInstance;
180+
}
165181
}
166182

167183
computeNextState() {
@@ -176,9 +192,15 @@ export default function createConnect(React) {
176192

177193
const finalProps = this.computeNextState();
178194

195+
if (!pure) {
196+
return <WrappedComponent {...finalProps} />;
197+
}
198+
179199
return (
180-
<WrappedComponent ref='wrappedInstance'
181-
{...finalProps} />
200+
<PureWrap
201+
ref='wrappedInstance'
202+
component={WrappedComponent}
203+
passProps={finalProps} />
182204
);
183205
}
184206
}

0 commit comments

Comments
 (0)