Skip to content

Commit 9992827

Browse files
committed
Style tweaks
1 parent 0949488 commit 9992827

File tree

2 files changed

+36
-18
lines changed

2 files changed

+36
-18
lines changed

src/components/createConnect.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import wrapActionCreators from '../utils/wrapActionCreators';
66
import invariant from 'invariant';
77

88
const emptySelector = () => ({});
9-
109
const emptyBinder = () => ({});
11-
12-
const identityMerge = (slice, actionsCreators, props) => ({ ...props, ...slice, ...actionsCreators});
10+
const identityMerge = (slice, actionsCreators, props) => ({
11+
...props,
12+
...slice,
13+
...actionsCreators
14+
});
1315

1416
function getDisplayName(Component) {
1517
return Component.displayName || Component.name || 'Component';
@@ -19,11 +21,17 @@ export default function createConnect(React) {
1921
const { Component, PropTypes } = React;
2022
const storeShape = createStoreShape(PropTypes);
2123

22-
return function connect(select, dispatchBinder = emptyBinder, mergeHandler = identityMerge) {
23-
24-
const subscribing = select ? true : false;
24+
return function connect(
25+
select,
26+
dispatchBinder = emptyBinder,
27+
mergeHandler = identityMerge
28+
) {
29+
const shouldSubscribe = select ? true : false;
2530
const selectState = select || emptySelector;
26-
const bindDispatch = isPlainObject(dispatchBinder) ? wrapActionCreators(dispatchBinder) : dispatchBinder;
31+
const bindDispatch = isPlainObject(dispatchBinder) ?
32+
wrapActionCreators(dispatchBinder) :
33+
dispatchBinder;
34+
2735
const merge = mergeHandler;
2836

2937
return DecoratedComponent => class ConnectDecorator extends Component {
@@ -41,31 +49,35 @@ export default function createConnect(React) {
4149

4250
isSliceEqual(slice, nextSlice) {
4351
const isRefEqual = slice === nextSlice;
44-
if (isRefEqual) {
45-
return true;
46-
} else if (typeof slice !== 'object' || typeof nextSlice !== 'object') {
52+
if (
53+
isRefEqual ||
54+
typeof slice !== 'object' ||
55+
typeof nextSlice !== 'object'
56+
) {
4757
return isRefEqual;
4858
}
59+
4960
return shallowEqual(slice, nextSlice);
5061
}
5162

5263
constructor(props, context) {
5364
super(props, context);
65+
this.setUnderlyingRef = ::this.setUnderlyingRef;
5466
this.state = {
5567
...this.selectState(props, context),
5668
...this.bindDispatch(context)
5769
};
5870
}
5971

6072
componentDidMount() {
61-
if (subscribing) {
73+
if (shouldSubscribe) {
6274
this.subscribed = true;
6375
this.unsubscribe = this.context.store.subscribe(::this.handleChange);
6476
}
6577
}
6678

6779
componentWillUnmount() {
68-
if (subscribing) {
80+
if (shouldSubscribe) {
6981
this.unsubscribe();
7082
}
7183
}
@@ -120,8 +132,15 @@ export default function createConnect(React) {
120132
return this.underlyingRef;
121133
}
122134

135+
setUnderlyingRef(instance) {
136+
this.underlyingRef = instance;
137+
}
138+
123139
render() {
124-
return <DecoratedComponent ref={component => (this.underlyingRef = component)} {...this.merge()} />;
140+
return (
141+
<DecoratedComponent ref={this.setUnderlyingRef}
142+
{...this.merge()} />
143+
);
125144
}
126145
};
127146
};

test/components/connect.spec.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ describe('React', () => {
215215
@connect(
216216
state => state,
217217
dispatch => ({ dispatch })
218-
)
218+
)
219219
class Container extends Component {
220220
render() {
221221
return <div {...this.props} />;
@@ -245,7 +245,7 @@ describe('React', () => {
245245
@connect(
246246
null,
247247
dispatch => ({ dispatch })
248-
)
248+
)
249249
class Container extends Component {
250250
render() {
251251
return <div {...this.props} />;
@@ -265,7 +265,6 @@ describe('React', () => {
265265
).toNotThrow();
266266
const decorated = TestUtils.findRenderedComponentWithType(container, Container);
267267
expect(decorated.subscribed).toNotBe(true);
268-
269268
});
270269

271270
it('should unsubscribe before unmounting', () => {
@@ -285,7 +284,7 @@ describe('React', () => {
285284
@connect(
286285
state => ({string: state}),
287286
dispatch => ({ dispatch })
288-
)
287+
)
289288
class Container extends Component {
290289
render() {
291290
return <div {...this.props} />;
@@ -317,7 +316,7 @@ describe('React', () => {
317316
@connect(
318317
state => ({string: state}),
319318
dispatch => ({ dispatch })
320-
)
319+
)
321320
class Container extends Component {
322321
render() {
323322
return render(this.props);

0 commit comments

Comments
 (0)