Skip to content

Commit 6e99536

Browse files
committed
Rename arguments
1 parent d8d416e commit 6e99536

File tree

2 files changed

+35
-37
lines changed

2 files changed

+35
-37
lines changed

src/components/createConnect.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import isPlainObject from '../utils/isPlainObject';
55
import wrapActionCreators from '../utils/wrapActionCreators';
66
import invariant from 'invariant';
77

8-
const emptySelector = () => ({});
9-
const defaultBinder = dispatch => ({ dispatch });
10-
const identityMerge = (slice, actionsCreators, props) => ({
8+
const defaultMapState = () => ({});
9+
const defaultMapDispatch = dispatch => ({ dispatch });
10+
const defaultMergeProps = (stateSlice, actionsCreators, props) => ({
1111
...props,
12-
...slice,
12+
...stateSlice,
1313
...actionsCreators
1414
});
1515

@@ -22,16 +22,14 @@ export default function createConnect(React) {
2222
const storeShape = createStoreShape(PropTypes);
2323

2424
return function connect(
25-
select,
26-
dispatchBinder = defaultBinder,
27-
mergeHandler = identityMerge
25+
mapState = defaultMapState,
26+
mapDispatchOrActionCreators = defaultMapDispatch,
27+
mergeProps = defaultMergeProps
2828
) {
29-
const shouldSubscribe = select ? true : false;
30-
const selectState = select || emptySelector;
31-
const bindDispatch = isPlainObject(dispatchBinder) ?
32-
wrapActionCreators(dispatchBinder) :
33-
dispatchBinder;
34-
const merge = mergeHandler;
29+
const shouldSubscribe = mapState !== defaultMapState;
30+
const mapDispatch = isPlainObject(mapDispatchOrActionCreators) ?
31+
wrapActionCreators(mapDispatchOrActionCreators) :
32+
mapDispatchOrActionCreators;
3533

3634
return DecoratedComponent => class ConnectDecorator extends Component {
3735
static displayName = `Connect(${getDisplayName(DecoratedComponent)})`;
@@ -63,8 +61,8 @@ export default function createConnect(React) {
6361
super(props, context);
6462
this.setUnderlyingRef = ::this.setUnderlyingRef;
6563
this.state = {
66-
...this.selectState(props, context),
67-
...this.bindDispatch(context)
64+
...this.mapState(props, context),
65+
...this.mapDispatch(context)
6866
};
6967
}
7068

@@ -82,32 +80,32 @@ export default function createConnect(React) {
8280
}
8381

8482
handleChange(props = this.props) {
85-
const nextState = this.selectState(props, this.context);
83+
const nextState = this.mapState(props, this.context);
8684
if (!this.isSliceEqual(this.state.slice, nextState.slice)) {
8785
this.setState(nextState);
8886
}
8987
}
9088

91-
selectState(props = this.props, context = this.context) {
89+
mapState(props = this.props, context = this.context) {
9290
const state = context.store.getState();
93-
const slice = selectState(state);
91+
const slice = mapState(state);
9492

9593
invariant(
9694
isPlainObject(slice),
97-
'The return value of `select` prop must be an object. Instead received %s.',
95+
'`mapState` must return an object. Instead received %s.',
9896
slice
9997
);
10098

10199
return { slice };
102100
}
103101

104-
bindDispatch(context = this.context) {
102+
mapDispatch(context = this.context) {
105103
const { dispatch } = context.store;
106-
const actionCreators = bindDispatch(dispatch);
104+
const actionCreators = mapDispatch(dispatch);
107105

108106
invariant(
109107
isPlainObject(actionCreators),
110-
'The return value of `bindDispatch` prop must be an object. Instead received %s.',
108+
'`mapDispatch` must return an object. Instead received %s.',
111109
actionCreators
112110
);
113111

@@ -116,11 +114,11 @@ export default function createConnect(React) {
116114

117115
merge(props = this.props, state = this.state) {
118116
const { slice, actionCreators } = state;
119-
const merged = merge(slice, actionCreators, props);
117+
const merged = mergeProps(slice, actionCreators, props);
120118

121119
invariant(
122120
isPlainObject(merged),
123-
'The return value of `merge` prop must be an object. Instead received %s.',
121+
'`mergeProps` must return an object. Instead received %s.',
124122
merged
125123
);
126124

test/components/connect.spec.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ describe('React', () => {
244244
expect(decorated.subscribed).toBe(true);
245245
});
246246

247-
it('should not subscribe to stores if select argument is falsy', () => {
247+
it('should not subscribe to stores if mapState argument is falsy', () => {
248248
const store = createStore(() => ({
249249
foo: 'bar'
250250
}));
@@ -346,12 +346,12 @@ describe('React', () => {
346346
expect(spy.calls.length).toBe(3);
347347
});
348348

349-
it('should throw an error if select, bindDispatch, or merge returns anything but a plain object', () => {
349+
it('should throw an error if mapState, mapDispatch, or mergeProps returns anything but a plain object', () => {
350350
const store = createStore(() => ({}));
351351

352-
function makeContainer(select, bindActionCreators, merge) {
352+
function makeContainer(mapState, mapDispatch, mergeProps) {
353353
return React.createElement(
354-
@connect(select, bindActionCreators, merge)
354+
@connect(mapState, mapDispatch, mergeProps)
355355
class Container extends Component {
356356
render() {
357357
return <div />;
@@ -368,71 +368,71 @@ describe('React', () => {
368368
{ () => makeContainer(() => 1, () => ({}), () => ({})) }
369369
</Provider>
370370
);
371-
}).toThrow(/select/);
371+
}).toThrow(/mapState/);
372372

373373
expect(() => {
374374
TestUtils.renderIntoDocument(
375375
<Provider store={store}>
376376
{ () => makeContainer(() => 'hey', () => ({}), () => ({})) }
377377
</Provider>
378378
);
379-
}).toThrow(/select/);
379+
}).toThrow(/mapState/);
380380

381381
expect(() => {
382382
TestUtils.renderIntoDocument(
383383
<Provider store={store}>
384384
{ () => makeContainer(() => new AwesomeMap(), () => ({}), () => ({})) }
385385
</Provider>
386386
);
387-
}).toThrow(/select/);
387+
}).toThrow(/mapState/);
388388

389389
expect(() => {
390390
TestUtils.renderIntoDocument(
391391
<Provider store={store}>
392392
{ () => makeContainer(() => ({}), () => 1, () => ({})) }
393393
</Provider>
394394
);
395-
}).toThrow(/bindDispatch/);
395+
}).toThrow(/mapDispatch/);
396396

397397
expect(() => {
398398
TestUtils.renderIntoDocument(
399399
<Provider store={store}>
400400
{ () => makeContainer(() => ({}), () => 'hey', () => ({})) }
401401
</Provider>
402402
);
403-
}).toThrow(/bindDispatch/);
403+
}).toThrow(/mapDispatch/);
404404

405405
expect(() => {
406406
TestUtils.renderIntoDocument(
407407
<Provider store={store}>
408408
{ () => makeContainer(() => ({}), () => new AwesomeMap(), () => ({})) }
409409
</Provider>
410410
);
411-
}).toThrow(/bindDispatch/);
411+
}).toThrow(/mapDispatch/);
412412

413413
expect(() => {
414414
TestUtils.renderIntoDocument(
415415
<Provider store={store}>
416416
{ () => makeContainer(() => ({}), () => ({}), () => 1) }
417417
</Provider>
418418
);
419-
}).toThrow(/merge/);
419+
}).toThrow(/mergeProps/);
420420

421421
expect(() => {
422422
TestUtils.renderIntoDocument(
423423
<Provider store={store}>
424424
{ () => makeContainer(() => ({}), () => ({}), () => 'hey') }
425425
</Provider>
426426
);
427-
}).toThrow(/merge/);
427+
}).toThrow(/mergeProps/);
428428

429429
expect(() => {
430430
TestUtils.renderIntoDocument(
431431
<Provider store={store}>
432432
{ () => makeContainer(() => ({}), () => ({}), () => new AwesomeMap()) }
433433
</Provider>
434434
);
435-
}).toThrow(/merge/);
435+
}).toThrow(/mergeProps/);
436436
});
437437

438438
it('should set the displayName correctly', () => {

0 commit comments

Comments
 (0)