Skip to content

Instantly share code, notes, and snippets.

@abruzzi
Forked from abhiaiyer91/reduxSelectorPattern.md
Created January 31, 2018 03:02
Show Gist options
  • Save abruzzi/3ce178d45f693d1692c53b06af21591f to your computer and use it in GitHub Desktop.
Save abruzzi/3ce178d45f693d1692c53b06af21591f to your computer and use it in GitHub Desktop.

Revisions

  1. @abhiaiyer91 abhiaiyer91 revised this gist Jun 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reduxSelectorPattern.md
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@ function mapStateToProps(state) {
    There are a couple problems with this approach as the application grows.

    1. The implementation of `incompleteItems` may change.
    2. Computation logic occurs in mapStateToProps
    2. Computation logic occurs in `mapStateToProps`
    3. Can't memoize the values of `incompleteItems`

    ## The Solution
  2. @abhiaiyer91 abhiaiyer91 revised this gist Jun 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reduxSelectorPattern.md
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ There are a couple problems with this approach as the application grows.

    ## The Solution

    After talking with Dan Abramov (founder of Redux) he has been preaching the colocation of functions called `selectors`.
    After talking with Dan Abramov (founder of Redux) he has been preaching the colocation of functions called `selectors` with your reducers.

    What is a `selector`?

  3. @abhiaiyer91 abhiaiyer91 revised this gist Jun 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reduxSelectorPattern.md
    Original file line number Diff line number Diff line change
    @@ -66,7 +66,7 @@ function listOfItems(state: Array<Object> = [], action: Object = {}): Array<Obje
    }
    }

    function getIncompleteItems(state) {
    function getIncompleteItems(state: Object): Array<Object> {
    return state.listOfItems.filter((item) => {
    return !item.completed
    });
  4. @abhiaiyer91 abhiaiyer91 revised this gist Jun 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reduxSelectorPattern.md
    Original file line number Diff line number Diff line change
    @@ -88,4 +88,4 @@ More importantly we can now memoize this state with [reselect](https://github.co

    ## Next Steps:

    Now don't go and rewrite all your `mapFns`! We'lluse this pattern going forward on new reducers and Redux `connect` components.
    Now don't go and rewrite all your `mapFns`! We'll use this pattern going forward on new reducers and Redux `connect` components.
  5. @abhiaiyer91 abhiaiyer91 revised this gist Jun 7, 2016. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions reduxSelectorPattern.md
    Original file line number Diff line number Diff line change
    @@ -57,6 +57,15 @@ Let's turn our filter into a `selector`.
    ### Place your selectors near the Redux reducer!!!

    ```js
    function listOfItems(state: Array<Object> = [], action: Object = {}): Array<Object> {
    switch(action.type) {
    case 'SHOW_ALL_ITEMS':
    return action.data.items
    default:
    return state;
    }
    }

    function getIncompleteItems(state) {
    return state.listOfItems.filter((item) => {
    return !item.completed
  6. @abhiaiyer91 abhiaiyer91 revised this gist Jun 7, 2016. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion reduxSelectorPattern.md
    Original file line number Diff line number Diff line change
    @@ -75,4 +75,8 @@ function mapStateToProps(state) {
    ```

    Now we can reuse this logic across many components mapping this exact state! We can unit test it as well!
    More importantly we can now memoize this state with [reselect](https://github.com/reactjs/reselect)
    More importantly we can now memoize this state with [reselect](https://github.com/reactjs/reselect)

    ## Next Steps:

    Now don't go and rewrite all your `mapFns`! We'lluse this pattern going forward on new reducers and Redux `connect` components.
  7. @abhiaiyer91 abhiaiyer91 revised this gist Jun 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reduxSelectorPattern.md
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ function mapStateToProps(state) {

    There are a couple problems with this approach as the application grows.

    1. If the implementation of `incompleteItems` may change.
    1. The implementation of `incompleteItems` may change.
    2. Computation logic occurs in mapStateToProps
    3. Can't memoize the values of `incompleteItems`

  8. @abhiaiyer91 abhiaiyer91 revised this gist Jun 7, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions reduxSelectorPattern.md
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@ function listOfItems(state: Array<Object> = [], action: Object = {}): Array<Obje
    }
    ```

    Where Items looks like this:
    Where `Items` looks like this:

    ```js
    type ItemType = {
    @@ -23,7 +23,7 @@ type ItemType = {
    };
    ```

    Today we mapStateToProps for all incomplete items like this:
    Today we `mapStateToProps` for all incomplete items like this:

    ```js
    function mapStateToProps(state) {
    @@ -52,7 +52,7 @@ What is a `selector`?
    1. Selectors can compute derived data, allowing Redux to store the minimal possible state.
    2. Selectors are composable. They can be used as input to other selectors.

    Let's turn our filter into a selector.
    Let's turn our filter into a `selector`.

    ### Place your selectors near the Redux reducer!!!

  9. @abhiaiyer91 abhiaiyer91 revised this gist Jun 7, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion reduxSelectorPattern.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    Imagine we have a reducer to control a list of items:

    ```js
    function listOfItems(state: Array<Object> = [], action = {}) {
    function listOfItems(state: Array<Object> = [], action: Object = {}): Array<Object> {
    switch(action.type) {
    case 'SHOW_ALL_ITEMS':
    return action.data.items
  10. @abhiaiyer91 abhiaiyer91 revised this gist Jun 7, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions reduxSelectorPattern.md
    Original file line number Diff line number Diff line change
    @@ -49,8 +49,8 @@ After talking with Dan Abramov (founder of Redux) he has been preaching the colo

    What is a `selector`?

    Selectors can compute derived data, allowing Redux to store the minimal possible state.
    Selectors are composable. They can be used as input to other selectors.
    1. Selectors can compute derived data, allowing Redux to store the minimal possible state.
    2. Selectors are composable. They can be used as input to other selectors.

    Let's turn our filter into a selector.

    @@ -75,4 +75,4 @@ function mapStateToProps(state) {
    ```

    Now we can reuse this logic across many components mapping this exact state! We can unit test it as well!
    More importantly we can now memoize this state with (reselect)[https://github.com/reactjs/reselect]
    More importantly we can now memoize this state with [reselect](https://github.com/reactjs/reselect)
  11. @abhiaiyer91 abhiaiyer91 revised this gist Jun 7, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion reduxSelectorPattern.md
    Original file line number Diff line number Diff line change
    @@ -72,4 +72,7 @@ function mapStateToProps(state) {
    incompleteItems: getIncompleteItems(state)
    }
    }
    ```
    ```

    Now we can reuse this logic across many components mapping this exact state! We can unit test it as well!
    More importantly we can now memoize this state with (reselect)[https://github.com/reactjs/reselect]
  12. @abhiaiyer91 abhiaiyer91 revised this gist Jun 7, 2016. 1 changed file with 75 additions and 1 deletion.
    76 changes: 75 additions & 1 deletion reduxSelectorPattern.md
    Original file line number Diff line number Diff line change
    @@ -1 +1,75 @@
    ## Yo
    ## Redux Selector Pattern

    Imagine we have a reducer to control a list of items:

    ```js
    function listOfItems(state: Array<Object> = [], action = {}) {
    switch(action.type) {
    case 'SHOW_ALL_ITEMS':
    return action.data.items
    default:
    return state;
    }
    }
    ```

    Where Items looks like this:

    ```js
    type ItemType = {
    id: string,
    text: string,
    completed: boolean
    };
    ```

    Today we mapStateToProps for all incomplete items like this:

    ```js
    function mapStateToProps(state) {
    return {
    incompleteItems: state.listOfItems.filter((item) => {
    return !item.completed
    });
    }
    }
    ```

    ## The problem with this approach

    There are a couple problems with this approach as the application grows.

    1. If the implementation of `incompleteItems` may change.
    2. Computation logic occurs in mapStateToProps
    3. Can't memoize the values of `incompleteItems`

    ## The Solution

    After talking with Dan Abramov (founder of Redux) he has been preaching the colocation of functions called `selectors`.

    What is a `selector`?

    Selectors can compute derived data, allowing Redux to store the minimal possible state.
    Selectors are composable. They can be used as input to other selectors.

    Let's turn our filter into a selector.

    ### Place your selectors near the Redux reducer!!!

    ```js
    function getIncompleteItems(state) {
    return state.listOfItems.filter((item) => {
    return !item.completed
    });
    }
    ```

    And we update our `mapStateToProps` function:

    ```js
    function mapStateToProps(state) {
    return {
    incompleteItems: getIncompleteItems(state)
    }
    }
    ```
  13. @abhiaiyer91 abhiaiyer91 renamed this gist Jun 7, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  14. @abhiaiyer91 abhiaiyer91 created this gist Jun 7, 2016.
    1 change: 1 addition & 0 deletions hello.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    ## Yo