-
-
Save abruzzi/3ce178d45f693d1692c53b06af21591f to your computer and use it in GitHub Desktop.
Revisions
-
abhiaiyer91 revised this gist
Jun 7, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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` 3. Can't memoize the values of `incompleteItems` ## The Solution -
abhiaiyer91 revised this gist
Jun 7, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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` with your reducers. What is a `selector`? -
abhiaiyer91 revised this gist
Jun 7, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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: Object): Array<Object> { return state.listOfItems.filter((item) => { return !item.completed }); -
abhiaiyer91 revised this gist
Jun 7, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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'll use this pattern going forward on new reducers and Redux `connect` components. -
abhiaiyer91 revised this gist
Jun 7, 2016 . 1 changed file with 9 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 -
abhiaiyer91 revised this gist
Jun 7, 2016 . 1 changed file with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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) ## Next Steps: Now don't go and rewrite all your `mapFns`! We'lluse this pattern going forward on new reducers and Redux `connect` components. -
abhiaiyer91 revised this gist
Jun 7, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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. The implementation of `incompleteItems` may change. 2. Computation logic occurs in mapStateToProps 3. Can't memoize the values of `incompleteItems` -
abhiaiyer91 revised this gist
Jun 7, 2016 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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: ```js type ItemType = { @@ -23,7 +23,7 @@ type ItemType = { }; ``` 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`. ### Place your selectors near the Redux reducer!!! -
abhiaiyer91 revised this gist
Jun 7, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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: Object = {}): Array<Object> { switch(action.type) { case 'SHOW_ALL_ITEMS': return action.data.items -
abhiaiyer91 revised this gist
Jun 7, 2016 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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`? 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) -
abhiaiyer91 revised this gist
Jun 7, 2016 . 1 changed file with 4 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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] -
abhiaiyer91 revised this gist
Jun 7, 2016 . 1 changed file with 75 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1 +1,75 @@ ## 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) } } ``` -
abhiaiyer91 renamed this gist
Jun 7, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
abhiaiyer91 created this gist
Jun 7, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1 @@ ## Yo