Skip to content

Commit 66e3097

Browse files
committed
Way simpler connect API
1 parent 7da4620 commit 66e3097

File tree

8 files changed

+44
-49
lines changed

8 files changed

+44
-49
lines changed

examples/containers/CounterApp.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import React from 'react';
2-
import { connect } from 'redux';
2+
import { connect, bindActions } from 'redux';
33
import Counter from '../components/Counter';
44
import * as CounterActions from '../actions/CounterActions';
55

6-
@connect({
7-
actions: CounterActions,
8-
select: state => ({
9-
counter: state.counter
10-
})
11-
})
6+
@connect(state => ({
7+
counter: state.counter
8+
}))
129
export default class CounterApp {
1310
render() {
11+
const { counter, dispatcher } = this.props;
1412
return (
15-
<Counter {...this.props} />
13+
<Counter counter={counter}
14+
{...bindActions(CounterActions, dispatcher)} />
1615
);
1716
}
1817
}

examples/containers/TodoApp.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import React from 'react';
2-
import { Connector } from 'redux';
2+
import { bindActions, Connector } from 'redux';
33
import AddTodo from '../components/AddTodo';
44
import TodoList from '../components/TodoList';
55
import * as TodoActions from '../actions/TodoActions';
66

77
export default class TodoApp {
88
render() {
99
return (
10-
<Connector actions={TodoActions}
11-
select={state => state.todos}>
10+
<Connector select={state => ({ todos: state.todos })}>
1211
{this.renderChild}
1312
</Connector>
1413
);
1514
}
1615

17-
renderChild({ state, actions }) {
16+
renderChild({ todos, dispatcher }) {
17+
const actions = bindActions(TodoActions, dispatcher);
1818
return (
1919
<div>
2020
<AddTodo {...actions} />
21-
<TodoList todos={state} {...actions} />
21+
<TodoList todos={todos} {...actions} />
2222
</div>
2323
);
2424
}

src/Dispatcher.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ export default class Dispatcher {
2626
this.setAtom(nextAtom);
2727
}
2828

29+
perform(actionCreator, ...args) {
30+
const action = actionCreator(...args);
31+
32+
return typeof action === 'function'
33+
? action(this.dispatch, this.atom)
34+
: this.dispatch(action);
35+
}
36+
2937
setAtom(atom) {
3038
this.atom = atom;
3139
this.emitChange();

src/components/Connector.js

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Component, PropTypes } from 'react';
22
import identity from 'lodash/utility/identity';
3-
import mapValues from 'lodash/object/mapValues';
43
import shallowEqual from '../utils/shallowEqual';
54

65
export default class Connector extends Component {
@@ -10,14 +9,10 @@ export default class Connector extends Component {
109

1110
static propTypes = {
1211
children: PropTypes.func.isRequired,
13-
select: PropTypes.func.isRequired,
14-
actions: PropTypes.objectOf(
15-
PropTypes.func.isRequired
16-
).isRequired
12+
select: PropTypes.func.isRequired
1713
};
1814

1915
static defaultProps = {
20-
actions: {},
2116
select: identity
2217
};
2318

@@ -48,15 +43,6 @@ export default class Connector extends Component {
4843
this.unsubscribe();
4944
}
5045

51-
performAction(actionCreator, ...args) {
52-
const { dispatch, atom } = this.context.redux;
53-
const payload = actionCreator(...args);
54-
55-
return typeof payload === 'function'
56-
? payload(dispatch, atom)
57-
: dispatch(payload);
58-
}
59-
6046
handleChange(atom) {
6147
const slice = this.props.select(atom);
6248
if (this.state) {
@@ -67,13 +53,13 @@ export default class Connector extends Component {
6753
}
6854

6955
render() {
70-
const { children, actions: _actions } = this.props;
71-
const { slice: state } = this.state;
72-
73-
const actions = mapValues(_actions, actionCreator =>
74-
this.performAction.bind(this, actionCreator)
75-
);
56+
const { children } = this.props;
57+
const { slice } = this.state;
58+
const { redux } = this.context;
7659

77-
return children({ state, actions });
60+
return children({
61+
dispatcher: redux,
62+
...slice
63+
});
7864
}
7965
}

src/components/Provider.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export default class Provider {
3333
return this.props.dispatcher.dispatch(action);
3434
}
3535

36+
perform(actionCreator, ...args) {
37+
return this.props.dispatcher.perform(actionCreator, ...args);
38+
}
39+
3640
render() {
3741
const { children } = this.props;
3842
return children();

src/components/connect.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,7 @@ import Connector from './Connector';
33
import getDisplayName from '../utils/getDisplayName';
44
import shallowEqualScalar from '../utils/shallowEqualScalar';
55

6-
function mergeAll({ props, state, actions }) {
7-
return { ...props, ...state, ...actions };
8-
}
9-
10-
export default function connect(
11-
{ actions: actionsToInject, select },
12-
getChildProps = mergeAll
13-
) {
6+
export default function connect(select) {
147
return DecoratedComponent => class ConnectorDecorator {
158
static displayName = `Connector(${getDisplayName(DecoratedComponent)})`;
169

@@ -24,18 +17,15 @@ export default function connect(
2417

2518
render() {
2619
return (
27-
<Connector actions={actionsToInject}
28-
select={select}>
20+
<Connector select={select}>
2921
{this.renderChild}
3022
</Connector>
3123
);
3224
}
3325

34-
renderChild({ state, actions }) {
26+
renderChild(state) {
3527
const { props } = this;
36-
const childProps = getChildProps({ props, state, actions });
37-
38-
return <DecoratedComponent {...childProps} />;
28+
return <DecoratedComponent {...props} {...state} />;
3929
}
4030
};
4131
}

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export connect from './components/connect';
1111

1212
// Utilities
1313
export composeStores from './utils/composeStores';
14+
export bindActions from './utils/bindActions';

src/utils/bindActions.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import mapValues from 'lodash/object/mapValues';
2+
3+
export default function bindActions(actionCreators, dispatcher) {
4+
return mapValues(actionCreators, actionCreator =>
5+
(...args) => dispatcher.perform(actionCreator, ...args)
6+
);
7+
}

0 commit comments

Comments
 (0)