Created
April 18, 2018 06:25
-
-
Save agoldis/a96e62fa32cff21a3d6ea6fb5ca0a949 to your computer and use it in GitHub Desktop.
redux - map state to props example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* initialState.js */ | |
const books = [ /* ... */ ] | |
const users = [ /* ... */ ] | |
const comments = [ /* ... */ ]; | |
const usersSummary = function() { | |
return this.users.map(user => ({ | |
...user, | |
booksCount: user.books.length, | |
commentsCount: this.comments.filter(c => c.user === user.id).length | |
})); | |
}; | |
export const initialState = { | |
books, | |
users, | |
comments, | |
get usersSummary() { | |
return usersSummary.call(this); | |
} | |
}; | |
/* Users.js component */ | |
import React from "react"; | |
import { connect } from "react-redux"; | |
const Users = ({ users }) => (/* ... */); | |
const mapStateToProps = ({ usersSummary }) => ({ users: usersSummary }); | |
export default connect(mapStateToProps)(Users); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment