Skip to content

Commit c7b2d60

Browse files
committed
Merge props of connected components
1 parent de96a33 commit c7b2d60

File tree

8 files changed

+23
-16
lines changed

8 files changed

+23
-16
lines changed

CHANGELOG.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
22

33
## Unreleased
44

5+
- Encourage passing the `props` object as the first argument to components.
6+
7+
Components are just functions so this isn't mandatory and you can still
8+
define arguments as you see fit. The pattern of passing `props` makes
9+
composition easier, encourages code which is more readable and decouples
10+
the implementation of the component from the action of connecting it to the
11+
store. From now on, connecting will only work for components with zero
12+
arity or which take an object as the first argument.
13+
514
- `connect` now takes a selector as an optional second argument.
615

716
You can now pass an optional selector function to `connect`. It will be
8-
passed the `state` and should return whatever the connected component
9-
expects as the first argument.
17+
passed the `state` and should return an object which will be merged with
18+
the component's `props` using `Object.assign({}, props, substate)`.
1019

1120
connect(FooComponent, state => state.foo);
1221

example01/ActiveList.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@ import { connect } from "./store";
33
import ActiveTask from "./ActiveTask";
44
import TaskInput from "./TaskInput";
55

6-
function ActiveList(state) {
7-
const { tasks } = state;
6+
function ActiveList({tasks}) {
87
return html`
98
<h2>My Active Tasks</h2>
109
<ul>
11-
${tasks.map(ActiveTask)}
10+
${tasks.map((task, index) => ActiveTask({task, index}))}
1211
<li>
1312
${TaskInput()}
1413
</li>

example01/ActiveTask.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import html from "../index";
22

3-
export default function ActiveTask(text, index) {
3+
export default function ActiveTask({task, index}) {
44
return html`
55
<li>
6-
${text}
6+
${task}
77
<button
88
onclick="dispatch('COMPLETE_TASK', ${index})">
99
Mark As Done</button>

example01/ArchivedList.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@ import html from "../index";
22
import { connect } from "./store";
33
import ArchivedTask from "./ArchivedTask";
44

5-
function ArchiveList(state) {
6-
const { archive } = state;
5+
function ArchiveList({archive}) {
76
return html`
87
<h2>Completed Tasks</h2>
98
<ul>
10-
${archive.map(ArchivedTask)}
9+
${archive.map(task => ArchivedTask({task}))}
1110
</ul>
1211
`;
1312
}

example01/ArchivedTask.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import html from "../index";
22

3-
export default function ArchivedTask(text) {
3+
export default function ArchivedTask({task}) {
44
return html`
55
<li style="color:#666; text-decoration:line-through">
6-
${text}
6+
${task}
77
</li>
88
`;
99
}

example03/App.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import html from "../index";
2-
import { connect } from "./store";
32

43
import TextInput from "./TextInput";
54
import CharCounter from "./CharCounter";
65

76
export default function App(idx) {
87
return html`
9-
${TextInput(idx)}
8+
${TextInput({idx})}
109
${CharCounter()}
1110
`;
1211
}

example03/TextInput.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import html from "../index";
22
import { connect } from "./store";
33

4-
function TextInput({value}, idx) {
4+
function TextInput({value, idx}) {
55
return html`
66
<textarea id="text-input-${idx}"
77
placeholder="Type here…"

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ export function createStore(reducer) {
4646
},
4747
connect(component, selector = state => state) {
4848
// Return a decorated component function.
49-
return (...args) => component(selector(state), ...args);
49+
return (props, ...args) =>
50+
component(Object.assign({}, props, selector(state)), ...args);
5051
},
5152
dispatch(action, ...args) {
5253
state = reducer(state, action, args);

0 commit comments

Comments
 (0)