Skip to content

Instantly share code, notes, and snippets.

@agoldis
Created April 18, 2018 06:25
Show Gist options
  • Save agoldis/a96e62fa32cff21a3d6ea6fb5ca0a949 to your computer and use it in GitHub Desktop.
Save agoldis/a96e62fa32cff21a3d6ea6fb5ca0a949 to your computer and use it in GitHub Desktop.
redux - map state to props example
/* 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