Skip to content

Commit 8c7bff1

Browse files
committed
docs: Add docs about using props as second argument to mapModelsToProps.
1 parent b721fc1 commit 8c7bff1

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ const UserModel = Backbone.Model.extend();
1111
const UserCollection = Backbone.Collection.extend({ model: UserModel });
1212

1313
const userInstance = new UserModel({ name: 'Harry', laughs: true });
14-
const userCollection = new UserCollection([userInstance]);
14+
const anotherUserInstance = new UserModel({ name: 'Samantha', laughs: false });
15+
const userCollection = new UserCollection([userInstance, anotherUserInstance]);
1516

1617
class MyComponent extends React.Component {
1718
render() {
@@ -32,13 +33,17 @@ class MyComponent extends React.Component {
3233

3334
// Maps Models to properties to give to the React Component. Optional.
3435
// Default behavior is to call `.toJSON()` on every Model and Collection.
35-
const mapModelsToProps = (models) => {
36+
// Second argument are props given to the React Component.
37+
const mapModelsToProps = (models, props) => {
3638
const { user, allUsers } = models;
39+
const { showOnlyLaughingUsers } = props;
3740

3841
// Everything returned from this function will be given as a prop to your Component.
3942
return {
4043
doesUserLaugh: user.get('laughs'),
41-
users: allUsers.toJSON(),
44+
users: showOnlyLaughingUsers ?
45+
allUsers.toJSON().filter(user => user.get('laughs') === true)
46+
allUsers.toJSON(),
4247
setUserLaughs(newVal) {
4348
user.set('laughs', newVal);
4449
},
@@ -87,7 +92,7 @@ const modelsMap = {
8792

8893
ReactDOM.render(
8994
// Pass the modelsMap to the HOC via the models prop.
90-
<MyComponentConnected models={modelsMap} />,
95+
<MyComponentConnected models={modelsMap} showOnlyLaughingUsers />,
9196
document.getElementById('app')
9297
);
9398
```

0 commit comments

Comments
 (0)