Skip to content

Instantly share code, notes, and snippets.

@rupeshtiwari
Last active September 1, 2017 20:21

Revisions

  1. rupeshtiwari renamed this gist Sep 1, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. rupeshtiwari created this gist Sep 1, 2017.
    50 changes: 50 additions & 0 deletions post-selector.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    // 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));