Last active
September 1, 2017 20:21
-
-
Save rupeshtiwari/f338035e67019cc87759d3e3a13a58b4 to your computer and use it in GitHub Desktop.
Reselect selector example for post object
This file contains 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
// 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