Skip to content

Commit 2345d80

Browse files
author
taylorhakes
committed
Removed shallowEqualScalar, simplified connect update logic
1 parent 406ca62 commit 2345d80

File tree

6 files changed

+102
-187
lines changed

6 files changed

+102
-187
lines changed

src/components/createConnectDecorator.js

-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import getDisplayName from '../utils/getDisplayName';
2-
import shallowEqualScalar from '../utils/shallowEqualScalar';
32

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

12-
shouldComponentUpdate(nextProps) {
13-
return !shallowEqualScalar(this.props, nextProps);
14-
}
15-
1611
render() {
1712
return (
1813
<Connector select={state => select(state, this.props)}>

src/components/createConnector.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,10 @@ export default function createConnector(React) {
2323
};
2424

2525
shouldComponentUpdate(nextProps, nextState) {
26-
return !this.isSliceEqual(this.state.slice, nextState.slice) ||
26+
return !shallowEqual(this.state.slice, nextState.slice) ||
2727
!shallowEqual(this.props, nextProps);
2828
}
2929

30-
isSliceEqual(slice, nextSlice) {
31-
const isRefEqual = slice === nextSlice;
32-
if (isRefEqual) {
33-
return true;
34-
} else if (typeof slice !== 'object' || typeof nextSlice !== 'object') {
35-
return isRefEqual;
36-
}
37-
return shallowEqual(slice, nextSlice);
38-
}
39-
4030
constructor(props, context) {
4131
super(props, context);
4232
this.state = this.selectState(props, context);
@@ -60,7 +50,7 @@ export default function createConnector(React) {
6050

6151
handleChange(props = this.props) {
6252
const nextState = this.selectState(props, this.context);
63-
if (!this.isSliceEqual(this.state.slice, nextState.slice)) {
53+
if (!shallowEqual(this.state.slice, nextState.slice)) {
6454
this.setState(nextState);
6555
}
6656
}

src/utils/shallowEqual.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ export default function shallowEqual(objA, objB) {
33
return true;
44
}
55

6+
if (typeof objA !== 'object' || objA === null ||
7+
typeof objB !== 'object' || objB === null) {
8+
return false;
9+
}
10+
611
const keysA = Object.keys(objA);
712
const keysB = Object.keys(objB);
813

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

1318
// Test for A's keys different from B.
14-
const hasOwn = Object.prototype.hasOwnProperty;
1519
for (let i = 0; i < keysA.length; i++) {
16-
if (!hasOwn.call(objB, keysA[i]) ||
17-
objA[keysA[i]] !== objB[keysA[i]]) {
20+
if (objA[keysA[i]] !== objB[keysA[i]]) {
1821
return false;
1922
}
2023
}

src/utils/shallowEqualScalar.js

-34
This file was deleted.

test/utils/shallowEqual.spec.js

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import expect from 'expect';
2+
import shallowEqual from '../../src/utils/shallowEqual';
3+
4+
describe('Utils', () => {
5+
// More info: https://github.com/gaearon/redux/pull/75#issuecomment-111635748
6+
describe('shallowEqual', () => {
7+
it('should return true if arguments fields are equal', () => {
8+
expect(
9+
shallowEqual(
10+
{ a: 1, b: 2, c: undefined },
11+
{ a: 1, b: 2, c: undefined }
12+
)
13+
).toBe(true);
14+
15+
expect(
16+
shallowEqual(
17+
{ a: 1, b: 2, c: 3 },
18+
{ a: 1, b: 2, c: 3 }
19+
)
20+
).toBe(true);
21+
22+
const o = {};
23+
expect(
24+
shallowEqual(
25+
{ a: 1, b: 2, c: o },
26+
{ a: 1, b: 2, c: o }
27+
)
28+
).toBe(true);
29+
expect(
30+
shallowEqual(
31+
null,
32+
null
33+
)
34+
).toBe(true);
35+
expect(
36+
shallowEqual(
37+
1,
38+
1
39+
)
40+
).toBe(true);
41+
});
42+
43+
it('should return false if first argument has too many keys', () => {
44+
expect(
45+
shallowEqual(
46+
{ a: 1, b: 2, c: 3 },
47+
{ a: 1, b: 2 }
48+
)
49+
).toBe(false);
50+
});
51+
52+
it('should return false if second argument has too many keys', () => {
53+
expect(
54+
shallowEqual(
55+
{ a: 1, b: 2 },
56+
{ a: 1, b: 2, c: 3 }
57+
)
58+
).toBe(false);
59+
});
60+
61+
it('should return false if arguments have different keys', () => {
62+
expect(
63+
shallowEqual(
64+
{ a: 1, b: 2, c: undefined },
65+
{ a: 1, bb: 2, c: undefined }
66+
)
67+
).toBe(false);
68+
});
69+
it('should return false if different values', () => {
70+
expect(
71+
shallowEqual(
72+
{ a: 1, b: 2, c: {} },
73+
{ a: 1, b: 2, c: {} }
74+
)
75+
).toBe(false);
76+
});
77+
it('should return true if both empty objects', () => {
78+
expect(
79+
shallowEqual(
80+
{},
81+
{}
82+
)
83+
).toBe(true);
84+
});
85+
it('should return false if object and null', () => {
86+
expect(
87+
shallowEqual(
88+
null,
89+
{}
90+
)
91+
).toBe(false);
92+
});
93+
});
94+
});

test/utils/shallowEquality.spec.js

-133
This file was deleted.

0 commit comments

Comments
 (0)