Skip to content

Instantly share code, notes, and snippets.

@rupeshtiwari
Last active September 1, 2017 20:21
Show Gist options
  • Save rupeshtiwari/f338035e67019cc87759d3e3a13a58b4 to your computer and use it in GitHub Desktop.
Save rupeshtiwari/f338035e67019cc87759d3e3a13a58b4 to your computer and use it in GitHub Desktop.
Reselect selector example for post object
// Code goes here
const {createSelector} = Reselect ;
const state = {
posts: [
{
title:'testing',
id: 1
},
{
title:'playing',
id: 2
},
{
title: 'dancing',
id: 3
},
{
title: 'singing',
id: 4
}
],
selectedPostIds : [
1,
3,
4
]
};
// selector
const getPosts = state => state.posts;
const getSelectedPostIds = state => state.selectedPostIds;
console.log( 'all posts' , getPosts(state) );
console.log( 'selected post ids' , getSelectedPostIds(state) );
// reselect selector
const getSelectedPosts = createSelector(
getPosts,
getSelectedPostIds,
(posts, ids) =>
posts.filter(p=>ids.indexOf(p.id)>-1)
);
console.log( 'selected posts' , getSelectedPosts(state));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment