Skip to content

Instantly share code, notes, and snippets.

@jackcallister
Created August 13, 2015 21:11
Show Gist options
  • Save jackcallister/7041fd30dc3fe62ed211 to your computer and use it in GitHub Desktop.
Save jackcallister/7041fd30dc3fe62ed211 to your computer and use it in GitHub Desktop.
// The Redux Store
{
posts : [{
...stuff,
userId: 1
tagIds: [1,2,3]
}],
users: [{
...stuff,
id: 1
}],
tags: [{
...stuff,
id: 1
}]
}
function selectPosts(store) {
return { posts: store.posts }
}
class PostsConnector {
render() {
return (
<Connector select={selectPosts}>
{({posts}) =>
<Posts posts={posts}
}
</Connector>
}
}
}
class Posts extends Component {
render() {
const posts = this.props.posts.map( post => {
return <PostConnecto {...post}/>
});
return (
<div>
{posts}
</div>
)
}
}
function selectUserAndTagsFromPost(post) {
return function(store) {
const user = store.users.find((user) => {
return user.id == post.userId;
});
const tags = store.tags.filter((tag) => {
return includes(post.tagIds, tag.id); // lodash or whatever
})
return { tags: tags, user: user };
}
}
class PostConnector {
<Connector select={selectUserAndTagsFromPost(post)}>
{({tags, user}) =>
<Post {...post} tags={tags} user={user} />
}
</Connector>
}
class Post extends Component {
render() {
const tags = this.props.tags.map(tag => {
return <Tag {...tag} />
});
<div>
{this.props.text}
<User {...this.props.user} />
<ul>
{tags}
</ul>
</div>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment