Skip to content

Removed shallowEqualScalar, simplified connect update logic #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/components/createConnectDecorator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import getDisplayName from '../utils/getDisplayName';
import shallowEqualScalar from '../utils/shallowEqualScalar';

export default function createConnectDecorator(React, Connector) {
const { Component } = React;
Expand All @@ -9,10 +8,6 @@ export default function createConnectDecorator(React, Connector) {
static displayName = `Connector(${getDisplayName(DecoratedComponent)})`;
static DecoratedComponent = DecoratedComponent;

shouldComponentUpdate(nextProps) {
return !shallowEqualScalar(this.props, nextProps);
}

render() {
return (
<Connector select={state => select(state, this.props)}>
Expand Down
14 changes: 2 additions & 12 deletions src/components/createConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,10 @@ export default function createConnector(React) {
};

shouldComponentUpdate(nextProps, nextState) {
return !this.isSliceEqual(this.state.slice, nextState.slice) ||
return !shallowEqual(this.state.slice, nextState.slice) ||
!shallowEqual(this.props, nextProps);
}

isSliceEqual(slice, nextSlice) {
const isRefEqual = slice === nextSlice;
if (isRefEqual) {
return true;
} else if (typeof slice !== 'object' || typeof nextSlice !== 'object') {
return isRefEqual;
}
return shallowEqual(slice, nextSlice);
}

constructor(props, context) {
super(props, context);
this.state = this.selectState(props, context);
Expand All @@ -60,7 +50,7 @@ export default function createConnector(React) {

handleChange(props = this.props) {
const nextState = this.selectState(props, this.context);
if (!this.isSliceEqual(this.state.slice, nextState.slice)) {
if (!shallowEqual(this.state.slice, nextState.slice)) {
this.setState(nextState);
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/utils/shallowEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ export default function shallowEqual(objA, objB) {
return true;
}

if (typeof objA !== 'object' || objA === null ||
typeof objB !== 'object' || objB === null) {
return false;
}

const keysA = Object.keys(objA);
const keysB = Object.keys(objB);

Expand All @@ -11,10 +16,8 @@ export default function shallowEqual(objA, objB) {
}

// Test for A's keys different from B.
const hasOwn = Object.prototype.hasOwnProperty;
for (let i = 0; i < keysA.length; i++) {
if (!hasOwn.call(objB, keysA[i]) ||
objA[keysA[i]] !== objB[keysA[i]]) {
if (objA[keysA[i]] !== objB[keysA[i]]) {
return false;
}
}
Expand Down
34 changes: 0 additions & 34 deletions src/utils/shallowEqualScalar.js

This file was deleted.

94 changes: 94 additions & 0 deletions test/utils/shallowEqual.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import expect from 'expect';
import shallowEqual from '../../src/utils/shallowEqual';

describe('Utils', () => {
// More info: https://github.com/gaearon/redux/pull/75#issuecomment-111635748
describe('shallowEqual', () => {
it('should return true if arguments fields are equal', () => {
expect(
shallowEqual(
{ a: 1, b: 2, c: undefined },
{ a: 1, b: 2, c: undefined }
)
).toBe(true);

expect(
shallowEqual(
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 2, c: 3 }
)
).toBe(true);

const o = {};
expect(
shallowEqual(
{ a: 1, b: 2, c: o },
{ a: 1, b: 2, c: o }
)
).toBe(true);
expect(
shallowEqual(
null,
null
)
).toBe(true);
expect(
shallowEqual(
1,
1
)
).toBe(true);
});

it('should return false if first argument has too many keys', () => {
expect(
shallowEqual(
{ a: 1, b: 2, c: 3 },
{ a: 1, b: 2 }
)
).toBe(false);
});

it('should return false if second argument has too many keys', () => {
expect(
shallowEqual(
{ a: 1, b: 2 },
{ a: 1, b: 2, c: 3 }
)
).toBe(false);
});

it('should return false if arguments have different keys', () => {
expect(
shallowEqual(
{ a: 1, b: 2, c: undefined },
{ a: 1, bb: 2, c: undefined }
)
).toBe(false);
});
it('should return false if different values', () => {
expect(
shallowEqual(
{ a: 1, b: 2, c: {} },
{ a: 1, b: 2, c: {} }
)
).toBe(false);
});
it('should return true if both empty objects', () => {
expect(
shallowEqual(
{},
{}
)
).toBe(true);
});
it('should return false if object and null', () => {
expect(
shallowEqual(
null,
{}
)
).toBe(false);
});
});
});
133 changes: 0 additions & 133 deletions test/utils/shallowEquality.spec.js

This file was deleted.