Skip to content

Commit af7d246

Browse files
committed
Avoid useless render calls
1 parent a072c66 commit af7d246

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

src/components/createConnect.js

Lines changed: 21 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,7 @@ export default function createConnect(React) {
161173
}
162174

163175
getWrappedInstance() {
164-
return this.refs.wrappedInstance;
176+
return this.refs.wrappedInstance.refs.wrappedInstance;
165177
}
166178

167179
computeNextState() {
@@ -176,9 +188,15 @@ export default function createConnect(React) {
176188

177189
const finalProps = this.computeNextState();
178190

191+
if (!pure) {
192+
return <WrappedComponent {...finalProps} />;
193+
}
194+
179195
return (
180-
<WrappedComponent ref='wrappedInstance'
181-
{...finalProps} />
196+
<PureWrap
197+
ref='wrappedInstance'
198+
component={WrappedComponent}
199+
passProps={finalProps} />
182200
);
183201
}
184202
}

0 commit comments

Comments
 (0)