Skip to content

Commit 389e57e

Browse files
author
Dallon Feldner
committed
Added pure: false option
1 parent 8b2049e commit 389e57e

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/components/createConnect.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const defaultMergeProps = (stateProps, dispatchProps, parentProps) => ({
1111
...stateProps,
1212
...dispatchProps
1313
});
14+
const defaultOptions = {
15+
pure: true
16+
};
1417

1518
function getDisplayName(Component) {
1619
return Component.displayName || Component.name || 'Component';
@@ -23,7 +26,7 @@ export default function createConnect(React) {
2326
const { Component, PropTypes } = React;
2427
const storeShape = createStoreShape(PropTypes);
2528

26-
return function connect(mapStateToProps, mapDispatchToProps, mergeProps) {
29+
return function connect(mapStateToProps, mapDispatchToProps, mergeProps, options) {
2730
const shouldSubscribe = Boolean(mapStateToProps);
2831
const finalMapStateToProps = mapStateToProps || defaultMapStateToProps;
2932
const finalMapDispatchToProps = isPlainObject(mapDispatchToProps) ?
@@ -32,6 +35,7 @@ export default function createConnect(React) {
3235
const finalMergeProps = mergeProps || defaultMergeProps;
3336
const shouldUpdateStateProps = finalMapStateToProps.length > 1;
3437
const shouldUpdateDispatchProps = finalMapDispatchToProps.length > 1;
38+
const finalOptions = {...defaultOptions, ...options} || defaultOptions;
3539

3640
// Helps track hot reloading.
3741
const version = nextVersion++;
@@ -88,7 +92,7 @@ export default function createConnect(React) {
8892
};
8993

9094
shouldComponentUpdate(nextProps, nextState) {
91-
return !shallowEqual(this.state.props, nextState.props);
95+
return !finalOptions.pure || !shallowEqual(this.state.props, nextState.props);
9296
}
9397

9498
constructor(props, context) {

test/components/connect.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ describe('React', () => {
10651065
expect(decorated.refs.wrappedInstance.someInstanceMethod()).toBe(someData);
10661066
});
10671067

1068-
it.only('should wrap impure components without supressing updates', () => {
1068+
it('should wrap impure components without supressing updates', () => {
10691069
const store = createStore(() => ({}));
10701070

10711071
class ImpureComponent extends Component {
@@ -1078,7 +1078,7 @@ describe('React', () => {
10781078
}
10791079
}
10801080

1081-
const decorator = connect(state => state);
1081+
const decorator = connect(state => state, null, null, { pure: false });
10821082
const Decorated = decorator(ImpureComponent);
10831083

10841084
class StatefulWrapper extends Component {

0 commit comments

Comments
 (0)