├── 1440 ├── react-redux-workflow-graphical-cheat-sheet-extended_v110.png └── react-redux-workflow-graphical-cheat-sheet_v110.png ├── 3840 ├── react-redux-workflow-graphical-cheat-sheet-extended_v110.png └── react-redux-workflow-graphical-cheat-sheet_v110.png ├── CHANGELOG.md ├── LICENSE.txt ├── README.md └── article └── react-redux-concept-workflow.md /1440/react-redux-workflow-graphical-cheat-sheet-extended_v110.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uanders/react-redux-cheatsheet/e5f6b79af6c108feda9b5f0c9ad647873775dd09/1440/react-redux-workflow-graphical-cheat-sheet-extended_v110.png -------------------------------------------------------------------------------- /1440/react-redux-workflow-graphical-cheat-sheet_v110.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uanders/react-redux-cheatsheet/e5f6b79af6c108feda9b5f0c9ad647873775dd09/1440/react-redux-workflow-graphical-cheat-sheet_v110.png -------------------------------------------------------------------------------- /3840/react-redux-workflow-graphical-cheat-sheet-extended_v110.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uanders/react-redux-cheatsheet/e5f6b79af6c108feda9b5f0c9ad647873775dd09/3840/react-redux-workflow-graphical-cheat-sheet-extended_v110.png -------------------------------------------------------------------------------- /3840/react-redux-workflow-graphical-cheat-sheet_v110.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/uanders/react-redux-cheatsheet/e5f6b79af6c108feda9b5f0c9ad647873775dd09/3840/react-redux-workflow-graphical-cheat-sheet_v110.png -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## Article 1.2.0 4 | 5 | + Added CHANGELOG.md 6 | + Changed code to be in line with `react-router` >= v4 7 | + Added two paragraphs to intro 8 | + Added remarks with respect to immutable.js 9 | 10 | ## Article 1.1.1 11 | 12 | + Remove whitespace 13 | + Add grammatical corrections 14 | + Remove spelling mistakes -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Attribution-ShareAlike 4.0 International 2 | 3 | ======================================================================= 4 | 5 | Creative Commons Corporation ("Creative Commons") is not a law firm and 6 | does not provide legal services or legal advice. Distribution of 7 | Creative Commons public licenses does not create a lawyer-client or 8 | other relationship. Creative Commons makes its licenses and related 9 | information available on an "as-is" basis. Creative Commons gives no 10 | warranties regarding its licenses, any material licensed under their 11 | terms and conditions, or any related information. Creative Commons 12 | disclaims all liability for damages resulting from their use to the 13 | fullest extent possible. 14 | 15 | Using Creative Commons Public Licenses 16 | 17 | Creative Commons public licenses provide a standard set of terms and 18 | conditions that creators and other rights holders may use to share 19 | original works of authorship and other material subject to copyright 20 | and certain other rights specified in the public license below. The 21 | following considerations are for informational purposes only, are not 22 | exhaustive, and do not form part of our licenses. 23 | 24 | Considerations for licensors: Our public licenses are 25 | intended for use by those authorized to give the public 26 | permission to use material in ways otherwise restricted by 27 | copyright and certain other rights. Our licenses are 28 | irrevocable. Licensors should read and understand the terms 29 | and conditions of the license they choose before applying it. 30 | Licensors should also secure all rights necessary before 31 | applying our licenses so that the public can reuse the 32 | material as expected. Licensors should clearly mark any 33 | material not subject to the license. This includes other CC- 34 | licensed material, or material used under an exception or 35 | limitation to copyright. More considerations for licensors: 36 | wiki.creativecommons.org/Considerations_for_licensors 37 | 38 | Considerations for the public: By using one of our public 39 | licenses, a licensor grants the public permission to use the 40 | licensed material under specified terms and conditions. If 41 | the licensor's permission is not necessary for any reason--for 42 | example, because of any applicable exception or limitation to 43 | copyright--then that use is not regulated by the license. Our 44 | licenses grant only permissions under copyright and certain 45 | other rights that a licensor has authority to grant. Use of 46 | the licensed material may still be restricted for other 47 | reasons, including because others have copyright or other 48 | rights in the material. A licensor may make special requests, 49 | such as asking that all changes be marked or described. 50 | Although not required by our licenses, you are encouraged to 51 | respect those requests where reasonable. More_considerations 52 | for the public: 53 | wiki.creativecommons.org/Considerations_for_licensees 54 | 55 | ======================================================================= 56 | 57 | Creative Commons Attribution-ShareAlike 4.0 International Public 58 | License 59 | 60 | By exercising the Licensed Rights (defined below), You accept and agree 61 | to be bound by the terms and conditions of this Creative Commons 62 | Attribution-ShareAlike 4.0 International Public License ("Public 63 | License"). To the extent this Public License may be interpreted as a 64 | contract, You are granted the Licensed Rights in consideration of Your 65 | acceptance of these terms and conditions, and the Licensor grants You 66 | such rights in consideration of benefits the Licensor receives from 67 | making the Licensed Material available under these terms and 68 | conditions. 69 | 70 | 71 | Section 1 -- Definitions. 72 | 73 | a. Adapted Material means material subject to Copyright and Similar 74 | Rights that is derived from or based upon the Licensed Material 75 | and in which the Licensed Material is translated, altered, 76 | arranged, transformed, or otherwise modified in a manner requiring 77 | permission under the Copyright and Similar Rights held by the 78 | Licensor. For purposes of this Public License, where the Licensed 79 | Material is a musical work, performance, or sound recording, 80 | Adapted Material is always produced where the Licensed Material is 81 | synched in timed relation with a moving image. 82 | 83 | b. Adapter's License means the license You apply to Your Copyright 84 | and Similar Rights in Your contributions to Adapted Material in 85 | accordance with the terms and conditions of this Public License. 86 | 87 | c. BY-SA Compatible License means a license listed at 88 | creativecommons.org/compatiblelicenses, approved by Creative 89 | Commons as essentially the equivalent of this Public License. 90 | 91 | d. Copyright and Similar Rights means copyright and/or similar rights 92 | closely related to copyright including, without limitation, 93 | performance, broadcast, sound recording, and Sui Generis Database 94 | Rights, without regard to how the rights are labeled or 95 | categorized. For purposes of this Public License, the rights 96 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 97 | Rights. 98 | 99 | e. Effective Technological Measures means those measures that, in the 100 | absence of proper authority, may not be circumvented under laws 101 | fulfilling obligations under Article 11 of the WIPO Copyright 102 | Treaty adopted on December 20, 1996, and/or similar international 103 | agreements. 104 | 105 | f. Exceptions and Limitations means fair use, fair dealing, and/or 106 | any other exception or limitation to Copyright and Similar Rights 107 | that applies to Your use of the Licensed Material. 108 | 109 | g. License Elements means the license attributes listed in the name 110 | of a Creative Commons Public License. The License Elements of this 111 | Public License are Attribution and ShareAlike. 112 | 113 | h. Licensed Material means the artistic or literary work, database, 114 | or other material to which the Licensor applied this Public 115 | License. 116 | 117 | i. Licensed Rights means the rights granted to You subject to the 118 | terms and conditions of this Public License, which are limited to 119 | all Copyright and Similar Rights that apply to Your use of the 120 | Licensed Material and that the Licensor has authority to license. 121 | 122 | j. Licensor means the individual(s) or entity(ies) granting rights 123 | under this Public License. 124 | 125 | k. Share means to provide material to the public by any means or 126 | process that requires permission under the Licensed Rights, such 127 | as reproduction, public display, public performance, distribution, 128 | dissemination, communication, or importation, and to make material 129 | available to the public including in ways that members of the 130 | public may access the material from a place and at a time 131 | individually chosen by them. 132 | 133 | l. Sui Generis Database Rights means rights other than copyright 134 | resulting from Directive 96/9/EC of the European Parliament and of 135 | the Council of 11 March 1996 on the legal protection of databases, 136 | as amended and/or succeeded, as well as other essentially 137 | equivalent rights anywhere in the world. 138 | 139 | m. You means the individual or entity exercising the Licensed Rights 140 | under this Public License. Your has a corresponding meaning. 141 | 142 | 143 | Section 2 -- Scope. 144 | 145 | a. License grant. 146 | 147 | 1. Subject to the terms and conditions of this Public License, 148 | the Licensor hereby grants You a worldwide, royalty-free, 149 | non-sublicensable, non-exclusive, irrevocable license to 150 | exercise the Licensed Rights in the Licensed Material to: 151 | 152 | a. reproduce and Share the Licensed Material, in whole or 153 | in part; and 154 | 155 | b. produce, reproduce, and Share Adapted Material. 156 | 157 | 2. Exceptions and Limitations. For the avoidance of doubt, where 158 | Exceptions and Limitations apply to Your use, this Public 159 | License does not apply, and You do not need to comply with 160 | its terms and conditions. 161 | 162 | 3. Term. The term of this Public License is specified in Section 163 | 6(a). 164 | 165 | 4. Media and formats; technical modifications allowed. The 166 | Licensor authorizes You to exercise the Licensed Rights in 167 | all media and formats whether now known or hereafter created, 168 | and to make technical modifications necessary to do so. The 169 | Licensor waives and/or agrees not to assert any right or 170 | authority to forbid You from making technical modifications 171 | necessary to exercise the Licensed Rights, including 172 | technical modifications necessary to circumvent Effective 173 | Technological Measures. For purposes of this Public License, 174 | simply making modifications authorized by this Section 2(a) 175 | (4) never produces Adapted Material. 176 | 177 | 5. Downstream recipients. 178 | 179 | a. Offer from the Licensor -- Licensed Material. Every 180 | recipient of the Licensed Material automatically 181 | receives an offer from the Licensor to exercise the 182 | Licensed Rights under the terms and conditions of this 183 | Public License. 184 | 185 | b. Additional offer from the Licensor -- Adapted Material. 186 | Every recipient of Adapted Material from You 187 | automatically receives an offer from the Licensor to 188 | exercise the Licensed Rights in the Adapted Material 189 | under the conditions of the Adapter's License You apply. 190 | 191 | c. No downstream restrictions. You may not offer or impose 192 | any additional or different terms or conditions on, or 193 | apply any Effective Technological Measures to, the 194 | Licensed Material if doing so restricts exercise of the 195 | Licensed Rights by any recipient of the Licensed 196 | Material. 197 | 198 | 6. No endorsement. Nothing in this Public License constitutes or 199 | may be construed as permission to assert or imply that You 200 | are, or that Your use of the Licensed Material is, connected 201 | with, or sponsored, endorsed, or granted official status by, 202 | the Licensor or others designated to receive attribution as 203 | provided in Section 3(a)(1)(A)(i). 204 | 205 | b. Other rights. 206 | 207 | 1. Moral rights, such as the right of integrity, are not 208 | licensed under this Public License, nor are publicity, 209 | privacy, and/or other similar personality rights; however, to 210 | the extent possible, the Licensor waives and/or agrees not to 211 | assert any such rights held by the Licensor to the limited 212 | extent necessary to allow You to exercise the Licensed 213 | Rights, but not otherwise. 214 | 215 | 2. Patent and trademark rights are not licensed under this 216 | Public License. 217 | 218 | 3. To the extent possible, the Licensor waives any right to 219 | collect royalties from You for the exercise of the Licensed 220 | Rights, whether directly or through a collecting society 221 | under any voluntary or waivable statutory or compulsory 222 | licensing scheme. In all other cases the Licensor expressly 223 | reserves any right to collect such royalties. 224 | 225 | 226 | Section 3 -- License Conditions. 227 | 228 | Your exercise of the Licensed Rights is expressly made subject to the 229 | following conditions. 230 | 231 | a. Attribution. 232 | 233 | 1. If You Share the Licensed Material (including in modified 234 | form), You must: 235 | 236 | a. retain the following if it is supplied by the Licensor 237 | with the Licensed Material: 238 | 239 | i. identification of the creator(s) of the Licensed 240 | Material and any others designated to receive 241 | attribution, in any reasonable manner requested by 242 | the Licensor (including by pseudonym if 243 | designated); 244 | 245 | ii. a copyright notice; 246 | 247 | iii. a notice that refers to this Public License; 248 | 249 | iv. a notice that refers to the disclaimer of 250 | warranties; 251 | 252 | v. a URI or hyperlink to the Licensed Material to the 253 | extent reasonably practicable; 254 | 255 | b. indicate if You modified the Licensed Material and 256 | retain an indication of any previous modifications; and 257 | 258 | c. indicate the Licensed Material is licensed under this 259 | Public License, and include the text of, or the URI or 260 | hyperlink to, this Public License. 261 | 262 | 2. You may satisfy the conditions in Section 3(a)(1) in any 263 | reasonable manner based on the medium, means, and context in 264 | which You Share the Licensed Material. For example, it may be 265 | reasonable to satisfy the conditions by providing a URI or 266 | hyperlink to a resource that includes the required 267 | information. 268 | 269 | 3. If requested by the Licensor, You must remove any of the 270 | information required by Section 3(a)(1)(A) to the extent 271 | reasonably practicable. 272 | 273 | b. ShareAlike. 274 | 275 | In addition to the conditions in Section 3(a), if You Share 276 | Adapted Material You produce, the following conditions also apply. 277 | 278 | 1. The Adapter's License You apply must be a Creative Commons 279 | license with the same License Elements, this version or 280 | later, or a BY-SA Compatible License. 281 | 282 | 2. You must include the text of, or the URI or hyperlink to, the 283 | Adapter's License You apply. You may satisfy this condition 284 | in any reasonable manner based on the medium, means, and 285 | context in which You Share Adapted Material. 286 | 287 | 3. You may not offer or impose any additional or different terms 288 | or conditions on, or apply any Effective Technological 289 | Measures to, Adapted Material that restrict exercise of the 290 | rights granted under the Adapter's License You apply. 291 | 292 | 293 | Section 4 -- Sui Generis Database Rights. 294 | 295 | Where the Licensed Rights include Sui Generis Database Rights that 296 | apply to Your use of the Licensed Material: 297 | 298 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 299 | to extract, reuse, reproduce, and Share all or a substantial 300 | portion of the contents of the database; 301 | 302 | b. if You include all or a substantial portion of the database 303 | contents in a database in which You have Sui Generis Database 304 | Rights, then the database in which You have Sui Generis Database 305 | Rights (but not its individual contents) is Adapted Material, 306 | 307 | including for purposes of Section 3(b); and 308 | c. You must comply with the conditions in Section 3(a) if You Share 309 | all or a substantial portion of the contents of the database. 310 | 311 | For the avoidance of doubt, this Section 4 supplements and does not 312 | replace Your obligations under this Public License where the Licensed 313 | Rights include other Copyright and Similar Rights. 314 | 315 | 316 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 317 | 318 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 319 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 320 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 321 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 322 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 323 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 324 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 325 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 326 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 327 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 328 | 329 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 330 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 331 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 332 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 333 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 334 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 335 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 336 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 337 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 338 | 339 | c. The disclaimer of warranties and limitation of liability provided 340 | above shall be interpreted in a manner that, to the extent 341 | possible, most closely approximates an absolute disclaimer and 342 | waiver of all liability. 343 | 344 | 345 | Section 6 -- Term and Termination. 346 | 347 | a. This Public License applies for the term of the Copyright and 348 | Similar Rights licensed here. However, if You fail to comply with 349 | this Public License, then Your rights under this Public License 350 | terminate automatically. 351 | 352 | b. Where Your right to use the Licensed Material has terminated under 353 | Section 6(a), it reinstates: 354 | 355 | 1. automatically as of the date the violation is cured, provided 356 | it is cured within 30 days of Your discovery of the 357 | violation; or 358 | 359 | 2. upon express reinstatement by the Licensor. 360 | 361 | For the avoidance of doubt, this Section 6(b) does not affect any 362 | right the Licensor may have to seek remedies for Your violations 363 | of this Public License. 364 | 365 | c. For the avoidance of doubt, the Licensor may also offer the 366 | Licensed Material under separate terms or conditions or stop 367 | distributing the Licensed Material at any time; however, doing so 368 | will not terminate this Public License. 369 | 370 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 371 | License. 372 | 373 | 374 | Section 7 -- Other Terms and Conditions. 375 | 376 | a. The Licensor shall not be bound by any additional or different 377 | terms or conditions communicated by You unless expressly agreed. 378 | 379 | b. Any arrangements, understandings, or agreements regarding the 380 | Licensed Material not stated herein are separate from and 381 | independent of the terms and conditions of this Public License. 382 | 383 | 384 | Section 8 -- Interpretation. 385 | 386 | a. For the avoidance of doubt, this Public License does not, and 387 | shall not be interpreted to, reduce, limit, restrict, or impose 388 | conditions on any use of the Licensed Material that could lawfully 389 | be made without permission under this Public License. 390 | 391 | b. To the extent possible, if any provision of this Public License is 392 | deemed unenforceable, it shall be automatically reformed to the 393 | minimum extent necessary to make it enforceable. If the provision 394 | cannot be reformed, it shall be severed from this Public License 395 | without affecting the enforceability of the remaining terms and 396 | conditions. 397 | 398 | c. No term or condition of this Public License will be waived and no 399 | failure to comply consented to unless expressly agreed to by the 400 | Licensor. 401 | 402 | d. Nothing in this Public License constitutes or may be interpreted 403 | as a limitation upon, or waiver of, any privileges and immunities 404 | that apply to the Licensor or You, including from the legal 405 | processes of any jurisdiction or authority. 406 | 407 | 408 | ======================================================================= 409 | 410 | Creative Commons is not a party to its public 411 | licenses. Notwithstanding, Creative Commons may elect to apply one of 412 | its public licenses to material it publishes and in those instances 413 | will be considered the “Licensor.” The text of the Creative Commons 414 | public licenses is dedicated to the public domain under the CC0 Public 415 | Domain Dedication. Except for the limited purpose of indicating that 416 | material is shared under a Creative Commons public license or as 417 | otherwise permitted by the Creative Commons policies published at 418 | creativecommons.org/policies, Creative Commons does not authorize the 419 | use of the trademark "Creative Commons" or any other trademark or logo 420 | of Creative Commons without its prior written consent including, 421 | without limitation, in connection with any unauthorized modifications 422 | to any of its public licenses or any other arrangements, 423 | understandings, or agreements concerning use of licensed material. For 424 | the avoidance of doubt, this paragraph does not form part of the 425 | public licenses. 426 | 427 | Creative Commons may be contacted at creativecommons.org. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-redux-cheatsheet 2 | 3 | Version Cheatsheet: 1.1.0
4 | Version Article: 1.2.0 5 | 6 | ## Overview 7 | 8 | This repository contains a graphical cheat sheet for the workflow and concept of Redux in two resolutions: 1440px and 3840px. 9 | 10 | The idea is to help new and existing Redux users to understand how the general mechanism of Redux works in some more detail and where to hook into this process. 11 | 12 | ![](1440/react-redux-workflow-graphical-cheat-sheet_v110.png) 13 | 14 | ## Article 15 | 16 | This repo furthermore holds an [article](/article/react-redux-concept-workflow.md) that walks you through an extended version of this cheat sheet. Two very common libraries have been added to the extended version: 'react-router' and 'immutable'. 17 | 18 | So, in order to supplement existing tutorials, this article describes the Redux conceptual overview and its workflow in a React Redux app. The description is starting with the dominant player in Redux Applications which is the store. Once the workflow is understood, it might probably be much easier to follow all of the above tutorials. 19 | 20 | While going the full circle the article also points to some of the common external libraries and how they would come into play: 'immutable', 'normalizr', 'reselect', 'redux-thunk', 'redux-saga', 'redux-promise' and 'redux-persist'. 21 | 22 | Please, raise an issue in case you find errors or things to be not clear enough. 23 | 24 | 25 | ## Notes 26 | 27 | Note, that a one page cheat sheet can not always reflect the general case. This holds true in the following cases: 28 | 29 | 1. The store holds the state of the app. The state of the app is typically sliced in slices. A state that is not sliced is a state with a single slice. 30 | 2. An *actionCreator()* can have a more general interface. If you want to adopt action creators with a common interface, read more about *payload* in the [*Flux Standard Action*](https://github.com/acdlite/flux-standard-action). 31 | 32 | ## Thanks 33 | 34 | In case you would like to read detailed comments on this cheat sheet you can go to [Redux issue #2254](https://github.com/reactjs/redux/issues/2254). 35 | 36 | Special thanks for improving and correcting this cheat sheet go to: 37 | 38 | + [markerikson](https://github.com/markerikson) 39 | 40 | + [naw](https://github.com/naw) 41 | 42 | + [sompylasar](https://github.com/sompylasar) 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /article/react-redux-concept-workflow.md: -------------------------------------------------------------------------------- 1 | # React Redux --- Concept, Workflow & Cheat Sheet 2 | 3 | Ulrich Anders 4 | 5 | Version 1.2.0, September 2017 6 | 7 | ## Preface 8 | 9 | This article contains a graphical cheat sheet for the workflow and concept of Redux. In line with this cheat sheet the purpose of this article is to help new and existing Redux users to understand how the general mechanism of Redux works in detail and where to hook into this process. 10 | 11 | This article is not an easy read. It is really for understanding the workflow of React Redux in detail. I recommend that you look at the graphical cheatsheet in parallel (print out, split screen, second monitor) while reading the article to always keep the overview. 12 | 13 | I am trying to keep the article current as much as possible, however, the most recent version will always be on [GitHub](https://github.com/uanders/react-redux-cheatsheet) together with a Changelog. 14 | 15 | 16 | ## Introduction 17 | 18 | [React](https://facebook.github.io/react/) and [Redux](http://redux.js.org/) really are some 19 | impressive developments and are certainly influencing how Frontend design is carried out now and in the foreseeable future. The principal concept of React is much easier to grasp than that of Redux. React deals with components that are much more imaginable. Redux on the other hand introduces a workflow that is much less naturally conceivable especially as it uses some vocabulary in its API that is not always intuitive.^[Naming an API so that it is semantically intuitive is an art. At the time, the Redux API has been chosen to mainly stay close to the API of Flux which was already quite known by then.] 20 | 21 | It is stated in the Redux documentation, that React does not need Redux and that it should not be used if it is not needed, but I think the opposite is true. Redux significantly reduces the complexity of an app, so in my opinion it really should be rather used than not used.^[I do not mean to suggest to always use Redux over MobX or Flux. I mean to always use Redux as opposed to not using Redux or alternatives.] I actually love to think that the name Redux is derived from REDUce compleXity. 22 | 23 | The price one has to pay for the reduction of complexity is, however, to learn the logic and vocabulary of an extra library. In addition to the official documentation, there are some really excellent tutorials out there about React with Redux. To name a few: 24 | 25 | 1. [Frontend -- Build your first real world React.js application. Post by Max Stoiber.](http://academy.plot.ly/react/1-introduction/) 26 | 1. [Leveling Up with React: Redux. Post by Brad Westfall](https://css-tricks.com/learning-react-redux/) 27 | 1. [Three Rules For Structuring (Redux) Applications. Series of posts by Jack Hsu. ](https://jaysoo.ca/2016/02/28/organizing-redux-application/) 28 | 1. [Getting Started with Redux. Video series by Dan Abramov.](https://egghead.io/courses/getting-started-with-redux) 29 | 1. [Building React Applications with Idiomatic Redux. Video series by Dan Abramov.](https://egghead.io/courses/building-react-applications-with-idiomatic-redux) 30 | 1. [Learn Redux. Video series by Wes Bos.](https://learnredux.com/) 31 | 1. [Learn React and Redux. Video series by Catalin Luntraru.](https://www.youtube.com/watch?v=d0oUGmSE6IY&list=PLJBrYU54JD2pTblB20OmV7GL6H5J-p2g8) 32 | 33 | Most tutorials approach the topic of Redux by building an app. While this is done the tutorials introduce the important concepts and workflow around Redux step by step. The general challenge with this approach is that two types of information are competing with each other: the conceptual overview and the details of the coding. 34 | 35 | So, in order to supplement existing tutorials, this article describes the Redux conceptual overview and its workflow in a React Redux app. The description is starting with the dominant player in Redux Applications which is the store. Once the workflow is understood, it will probably be much easier to follow all of the above tutorials. 36 | 37 | While going the full circle this article also points to some of the common external libraries and how they would come into play: 'immutable', 'normalizr', 'reselect', 'redux-thunk', 'redux-saga', 'redux-promise' and 'redux-persist'. 38 | 39 | ## A Graphical Cheat Sheet 40 | 41 | 42 | I'd like to start with a graphical cheat sheet explaining the workflow in a React Redux app. For those who already know React Redux it just serves as a reminder. For all others, once you have read this article you will understand the workflow pretty sure. 43 | 44 | ![](/1440/react-redux-workflow-graphical-cheat-sheet-extended_v110.png) 45 | 46 | 47 | 48 | ## Store 49 | 50 | 1. The central idea of a Redux app is to separate the state of the app from the app itself. 51 | 1. The state of the app is stored in a [store](http://redux.js.org/docs/basics/Store.html). I therefore distinguish between the state OF the app which is IN the store. 52 | 1. The store is THE dominante player in a React Redux app. 53 | 1. The state stored in the store is a simple JS object with slices. As the state thus hierarchically contains other structures it also called a state tree: 54 | 55 | ```JavaScript 56 | // this uses ES6 shorthand syntax 57 | state = { 58 | slice01, 59 | slice02, 60 | /* ... */ 61 | sliceN 62 | } 63 | ``` 64 | 65 | 1. Every slice can have its own data type: number, string, array and of course object. For the purpose of this tutorial I assume that all slices are of type object. For managing the state of your app you will find, that it is much easier to access parts of this objects if it is normalized. Read more about normalization in the library [*normalizr*](https://github.com/paularmstrong/normalizr). 66 | 67 | ```JavaScript 68 | // bad: 69 | "users": { 70 | {"id": "1", "name": "Adam"}, 71 | {"id": "2", "name": "Eve"}, 72 | } 73 | 74 | //good: 75 | "users": { 76 | "1": { "id": "1", "name": "Adam" }, 77 | "2": { "id": "2", "name": "Eve" } 78 | }, 79 | ``` 80 | 81 | 82 | 1. Every slice should be immutable. That means it cannot be changed. If the state of the app changes, the old state object has to be updated into a new state object that contains everything from the current state object and the updated slice. 83 | 84 | 1. To help manage the immutability and also make the updates fast it is sensible to use for instance [*immutable.js*](https://facebook.github.io/immutable-js/docs/#/). This library provides a function [*fromJS()*](https://facebook.github.io/immutable-js/docs/#/fromJS) that generates an immutable object from a JS object. I have assumed here that all slices are JS objects, which is most often the case in complex apps. But of course, other data structures are possible instead. 85 | 86 | ```JavaScript 87 | import { fromJS } from 'immutable' 88 | 89 | state = { 90 | slice01: fromJS(slice01) 91 | slice02: fromJS(slice02), 92 | /* ... */ 93 | sliceN: fromJS(SliceN) 94 | } 95 | ``` 96 | 97 | 1. There are some pros and cons using *immutable.js* which you can find in the [Redux recipes for *immutable.js*](http://redux.js.org/docs/recipes/UsingImmutableJS.html). I agree with all of them but I differ with respect to putting the whole state into immutable.js. Instead, I recommend to use immutable.js only within a slice which gives you much more flexibility and preserves the logic of the state being split in slices, where each slice can be accessed and managed separately. 98 | 99 | 1. Every slice of the state is managed by its own function, that is responsible for updating exactly this slice and copying all other slices into the new state object. This function is called a [*reducer()*](http://redux.js.org/docs/basics/Reducers.html). The name *reducer* is semantically not very meaningful and has a purely technical origin.^[Quoting from the official documentation: "It's called a reducer because it's the type of function you would pass to Array.prototype.reduce(reducer, ?initialValue)."] If you struggle with the name translate it into *producer*, because its only purpose is to just produce a new state that is put in the store. 100 | 101 | 1. It is a [popular convention](http://redux.js.org/docs/api/combineReducers.html) (but not an obligation) to name a reducer function after the slice it manages. I suggest, that you really stick to this convention, because it is helpful in many respects when you go in. So a reducer for *state.slice01* would be called *slice01()*. 102 | 103 | 1. Since there usually is a reducer for each slice, we end up with a lot of individual slice reducers where each of them is only concerned with its slice. However, we need to take care of all the slices at the same time. The solution is to combine all slice reducers into one overall reducer that is often named *rootReducer()*. To achieve this there is a function called [*combineReducers()*](http://redux.js.org/docs/api/combineReducers.html) that takes an object containing the individal reducers: 104 | 105 | ```JavaScript 106 | import { combineReducers } from 'redux' 107 | 108 | const rootReducer = function combineReducers({ 109 | slice01, 110 | slice02, 111 | /* ... */ 112 | sliceN 113 | }) 114 | ``` 115 | 116 | 1. Note, since *combineReducers()* takes an object of reducers you can also build subsets of reducers with *combineReducers()* and then combine these subsets into the *rootReducer()*. 117 | 118 | 119 | ## App & Components 120 | 121 | 1. A React Redux app contains components nested in components. 122 | 123 | 1. When a parent component has a child component the parent can pass down data to its child via key-value-pairs. These data become properties of the child component and are therefore accessible from within the child component via the property keyword [*props*](https://facebook.github.io/react-native/docs/props.html). 124 | 125 | 1. As the store is a component completely separate from the app, the store needs to become known to the app. This is achieved by passing the store from the Root component to the App component. Within the App component the store would now be accessible with *this.props.store*. 126 | 127 | ```JSX 128 | const Root = ( {store} ) => ( 129 | 130 | ) 131 | ``` 132 | 133 | 1. The store could now be passed down from the App component to all its child components. But this is exactly not what we want, because not all components would need the store at all or all of it. 134 | 135 | 1. There are two types of components: [smart (container) components and dumb (presentational) components](https://medium.com/@dan_abramov/smart-and-dumb-components-7ca2f9a7c7d0#.3upgdk21p). Smart components have state, dumb components don't. Of course, there are exemptions to this rule. In the context of Redux you can interpret that smart components have outsourced the state they originally possessed to an external state manager which is the store. 136 | 137 | 1. Smart components are managing data, making calculations or handling events. They also embed dumb components and pass down data or functions needed by the dumb components. 138 | 139 | ```JSX 140 | class SmartComponent01 extends Component { 141 | manageSomeData () { 142 | /* ... */ 143 | } 144 | makeSomeCalculations () { 145 | /* ... */ 146 | } 147 | handleSomeEvent = (event) => { 148 | /* ... */ 149 | } 150 | 151 | render() { 152 | return ( 153 |
154 | 155 | 156 |
157 | ) 158 | } 159 | } 160 | ``` 161 | 162 | Just as a side note, since this is the first time we are showing class methods: remember, that there are two types of notations for method declarations with a different effect to [*autobinding*](https://facebook.github.io/react/docs/react-without-es6.html#autobinding) the methods to the [*this*](http://exploringjs.com/es6/ch_arrow-functions.html#sec_traditional-functions-bad-non-methods) operator of the classes. Here, you would need to bind *manageSomeData()* and *makeSomeCalculations()* manually to the *this* operator, whereas *handleSomeEvent()* would have been autobound. 163 | 164 | 165 | 1. Since only smart container components manage data only they need to receive state data from the store. This works via a function called [*connect()*](https://github.com/reactjs/react-redux/blob/master/docs/api.md#connectmapstatetoprops-mapdispatchtoprops-mergeprops-options) which you need to import from 'react-redux'. The *connect()* function returns a higher order component, i.e. a component that expects a component as an argument. This argument is the smart component that you want to connect. The higher order component created by the *connect()* function renders the smart component passing the data from the store into its props. 166 | 167 | ```JavaScript 168 | import {connect} from 'react-redux' 169 | 170 | /* ... */ 171 | 172 | const ConnectedSmartComponent01 = connect()(SmartComponent01) 173 | export default ConnectedSmartComponent01 174 | 175 | ``` 176 | 177 | 1. Even though your smart component is now connected, it does not yet receive any state data from the store. So you need to pass it the required state data. But where do you map it to? Well, remember components have properties and they are named *props*. 178 | 179 | 1. Thus, the mapping from a state slice to the properties of a smart component is done with a function that is conventionally named [*mapStateToProps()*](http://redux.js.org/docs/basics/UsageWithReact.html). Per connected component you'll need to write such a function, that will return the state data of the slice(s) needed by the component. So, in the end you will have plenty of *mapStateToProps()* functions all with the same name, but a different content. The same name is not a problem, since none of the *mapStateToProps()* functions are ever exported. So, for the component to now actually receive the state data via the *connect()* function you need to pass *mapStateToProps()* to the *connect()* function. By doing this, the connected component is then subscribing to Redux state updates. 180 | 181 | ```JavaScript 182 | import {connect} from 'react-redux' 183 | 184 | function mapStateToProps(state) { 185 | return { 186 | // component gets this.props.slice01 187 | slice01: state.slice01 188 | } 189 | } 190 | 191 | // export without a new name 192 | export default connect(mapStateToProps)(SmartComponent01) 193 | ``` 194 | 195 | 1. Whenever the state in the store is updated, all your *mapStateToProps()* mapping functions for connecting components will be called and the new state data mapped to the properties of the connected components. 196 | 197 | 1. If you only want to map a subset of your slice to certain properties in your component you can make a selection from the slice of your state tree. A function that makes a selection is called a *selector*. It is a convention to name selector functions with an initial *get...*. But remember each time the store gets an update all mapping functions are called independent of whether the state change is relevant for a component. That means that also all the selector functions are called. If a selector function now is computationally expensive, it might slow down your app. In this case you should optimize your selectors by help of ['reselect'](https://github.com/reactjs/reselect). 198 | 199 | ```JavaScript 200 | function mapStateToProps(state) { 201 | return { 202 | // component gets this.props.selection1 203 | selection1: getSelection1(state.slice01), 204 | // component gets this.props.selection2 205 | selection2: getSelection2(state.slice01) 206 | } 207 | } 208 | ``` 209 | 210 | 1. Coming back to the *connect()* function. Even though you have imported the connect function properly from 'react-redux' you may wonder how the *connect()* function actually is able to access the store across the whole app even though the store is not explicitly passed down to all child components. The answer sits with a component called [*Provider*](https://github.com/reactjs/react-redux/blob/master/docs/api.md#provider-store) that you must wrap around your App component. The sole purpose of the *Provider* is to add the store to the context of the App component, so that all child components can access it. [*Context*](https://facebook.github.io/react/docs/context.html) is a special feature of React for such rare cases. With this said, the *connect()* function can now access the store methods. Thus, the Provider component replaces the root component that we introduced in the beginning: 211 | 212 | ```JSX 213 | import { Provider } from 'react-redux' 214 | 215 | const Root = ( {store} ) => ( 216 | 217 | 218 | 219 | ) 220 | ``` 221 | 222 | ## Reducers & Actions 223 | 224 | 1. Now, say, the user clicks on a button or inputs some information that changes the state of your application. In this case you want to issue a command that the state of your store is to be updated. Well, for issuing such a command there is a function called [*dispatch()*](http://redux.js.org/docs/api/Store.html#dispatch). This function is a method that belongs to the store, which means the store not only holds the state of the app it also operates as a dispatcher that dispatches commands. 225 | 226 | 1. But how then can you access *dispatch()* from within a component when it belongs to the store? The answer sits with the *connect()* function. Whenever you connect a component to the store, the *connect()* function takes the stores *dispatch()* method and implicitly injects it into the component by mapping it to its properties. So from within the connected component it is accessible like so: 227 | 228 | ```JavaScript 229 | this.props.dispatch 230 | ``` 231 | 232 | 1. If you do not want the *connect()* function to just inject the standard *dispatch()* function and map it to *this.props.dispatch* you can modify it according to your needs. This is achieved by help of a function, that is conventionally named *mapDispatchToProps()*. After writing it you need to hand it over to the *connect()* function as the second argument. 233 | 234 | ```JavaScript 235 | function mapDispatchToProps(dispatch) { 236 | return { 237 | /* your own bindings for the dispatch() function */ 238 | } 239 | } 240 | 241 | export default connect(mapStateToProps, 242 | mapDispatchToProps)(SmartComponent01) 243 | ``` 244 | 245 | 1. Your component now has a *dispatch()* function. But what is the *dispatch()* function dispatching? A dispatcher normally dispatches a command to someone. Think of 911, where the dispatcher commands a police officer to undertake something. Or in a shop, where the dispatcher commands a good to be send to a customer. In Redux you need different commands, but they all relate to updating the state in the store. Each command therefore needs to contain two kinds of information. A type to represent the command and the data relevant for the state change. This command is called an [*action*](http://redux.js.org/docs/basics/Actions.html) in Redux. The name *action* may be confusing at first, since with this name one would rather expect a function than an object. However, with time you get used to it. The *action* is a plain JS object containing a type and ideally the minimal amount of *relevant* data required to perform the state change. 246 | 247 | 248 | ```JavaScript 249 | const DO_SOMETHING = 'DO_SOMETHING' 250 | 251 | // an action object 252 | { 253 | type: DO_SOMETHING, 254 | payload: relevantData 255 | } 256 | ``` 257 | 258 | 1. Here, I am explicitly saying *relevant* data. The *relevant* data are not necessarily the changing state data itself. The relevant data could also contain an ID, for instance, to find the corresponding state data in the state tree. It is a convention to specify the action types in capital letters. It is not a pre-requisite, but many people assign the relevant data to a key that is often named [*payload*](https://github.com/acdlite/flux-standard-action) to have the same API for all action objects. 259 | 260 | ```JavaScript 261 | // relevant data 262 | let payload = 263 | { 264 | id, 265 | something 266 | } 267 | ``` 268 | 269 | 1. It is not so convenient to deal with action objects, so typically one uses a function, that returns the action object. This function has the name [*actionCreator()*](http://redux.js.org/docs/basics/Actions.html#action-creators). The name is a little bit pompous, because the function is not really creating much, it is just returning an action object --- that's it. 270 | 271 | ```JavaScript 272 | // this simple function is called an actionCreator 273 | function do_something(payload) { 274 | return ( 275 | { 276 | type: DO_SOMETHING, 277 | payload 278 | } 279 | ) 280 | } 281 | ``` 282 | 283 | 1. If the *dispatch()* function is now dispatching a command only in form of an object, which function is then actually receiving this command and dealing with updating the store? We are coming back to our reducer functions that we introduced earlier. Remember, that the purpose of the reducer functions is to produce a new slice of the state, if there is a change within this slice. So this means the *dispatch()* function has to call the reducer function. And this is exactly what happens underneath in the Redux code. So within its function body the *dispatch()* function calls the *rootReducer()* function and passes over the command what to do in form of the *action* object as the second argument. 284 | 285 | 1. And the first argument? A *rootReducer()* obviously, needs two arguments in order to return a new state: *rootReducer = (state, action) => newState*. The first argument is the current state in the store and the second argument represents the changes to it. And where does the *rootReducer()* now get the current state from? Well, again from the *dispatch()* function. It first fetches the current state from the store with *store.getState()* method and then passes the current state to the *rootReducer()* as a first argument together with the *action* object as a second argument. 286 | 287 | 1. The *rootReducer()* takes the received action and hands it down to all its child reducers, however, not with the whole state but only with its corresponding slice of the state: *sliceReducer = (slice, action) => newSlice*.^[Note, other than the [*Redux Documentation*](http://redux.js.org/docs/basics/Reducers.html#handling-actions) reads *(previousState, action) => newState* a reducer does not receive the whole state by default but only its slice of the state.] Each slice reducer compares the received *action.type* to the cases it has within its function body. If no match is found it returns the current state of the slice and nothing changes. If there is a match it computes the update of the according slice and returns it, so that an overall new state with an updated slice can be generated. 288 | 289 | ```JavaScript 290 | // reducer for state.slice01 291 | // using setIn() from immutable.js 292 | function slice01(slice01 = {}, action) { 293 | switch (action.type) { 294 | case DO_SOMETHING: 295 | return slice01.setIn(['somewhere', action.payload.id], 296 | action.payload.something) 297 | default: 298 | return slice01 299 | } 300 | } 301 | ``` 302 | 303 | 304 | 1. In this way, the store receives a new state. When the store is updated with a new state a re-render of the components that have subscribed to the store is triggered. 305 | 306 | 1. Note, that action creators are most often used to create actions that lead to a change of the state in the store. But not all action creators are used to generate state changes that are stored. Some action objects are generated by action creators just to make temporary state changes to the app which are not stored. 307 | 308 | 1. An arbitrary number of functions can register at the store to be also triggered whenever the store got updated with a new state. The functions registered at the store to be triggered have been named listeners, probably because they are "listening" to updates of the state. But in fact, they are not actively listening themselves. Quite the opposite, they are not actively in control, they are just passively triggered. The triggering function is a method that belongs to the store named [*store.subscribe()*](http://redux.js.org/docs/api/Store.html#subscribe). This name also takes a little bit getting used to as the store itself actually does not subscribe to anything. So, mentally you could think *trigger* when you use *subscribe()*.^[Don't confuse a component actually *subscribing* to a state change by help of the *connect()* function with a function that can be registered with the *store.subscribe()* method in order to be triggered whenever there is a new state.] 309 | 310 | ```JavaScript 311 | store.subscribe(listener) 312 | ``` 313 | 314 | 1. This completes the cycle. 315 | 316 | 317 | ## Round up 318 | 319 | 1. Even though we started this article with the store, we have not spoken about how to create it. This was because we needed to introduce the concept of reducers first. Now, that we know what reducers are, we can set up the store with [*createStore()*](http://redux.js.org/docs/api/createStore.html). At the minimum the store needs one argument which is the *rootReducer()*. Why? Remember, that later on the *store.dispatch()* function is calling the *rootReducer()*, so it needs to know it. As an optional second argument you can pre-populate the store with an initial state: 320 | 321 | ```JavaScript 322 | import { createStore } from 'redux' 323 | 324 | let store = createStore(rootReducer, initialState) 325 | ``` 326 | 327 | 1. If you want to save the store to local storage or re-hydrate the store when you restart your app it may be well worth to have a look at ['redux-persist'](https://github.com/rt2zz/redux-persist). This library also helps you with immutable transformations and with encrypting your data in the local storage. 328 | 329 | 1. Sometimes you want to include additional functionality whenever an action command is issued by the *dispatch()* function. This could for instance be a logger function or a timeout scheduler. For this purpose Redux offers an interface to so called [*middleware*](http://redux.js.org/docs/advanced/Middleware.html). The middleware is executed after the *dispatch()* and before the *rootReducer()* function. That means, each time when the *dispatch()* function is issuing an action command, the middleware is performed before the *rootReducer()* gets to work. To set this up, there is a third optional argument in *createStore()* called *enhancer*. This argument expects a function that contains the individual middleware functions. Redux provides for such a function called [*applyMiddleware()*](http://redux.js.org/docs/api/applyMiddleware.html). You can now pass your enhancements as arguments to *applyMiddleware()* and they are registered so that they are executed each time the *dispatch()* function is called. Obviously, every middleware function has to comply to the middleware function specification, since the output of the first middleware function is becoming the input for the next. The sequence in which they are executed is from left to right. The last function that is executed is actually the *dispatch()* function itself. 330 | 331 | ```JavaScript 332 | import { createStore, applyMiddleware } from 'redux' 333 | 334 | let store = createStore( 335 | rootReducer, 336 | applyMiddleware(logger, timeoutScheduler) 337 | ) 338 | ``` 339 | 340 | 1. Many extensions to Redux exploit this middleware functionality. If you want to stack further middleware on top of e.g. the *applyMiddleware()* you can use the [*compose()*](http://redux.js.org/docs/api/compose.html) utility function. 341 | 342 | 343 | ```JavaScript 344 | let store = createStore( 345 | rootReducer, 346 | compose( 347 | applyMiddleware(logger, timeoutScheduler), 348 | middlewareFromAnExtension() 349 | ) 350 | ) 351 | ``` 352 | 353 | 1. React and Redux are synchronous by nature, that means they go about their things sequentially. But what happens, if the *dispatch()* is issuing an action command to the root reducer, but the relevant data are not immediately available and you need to wait for them to arrive from an external API? Or if you want to *dispatch()* an action command which data are based on a promise? For these cases Redux has additional libraries such as ['redux-thunk'](https://github.com/gaearon/redux-thunk), ['redux-promise'](https://www.npmjs.com/package/redux-promise) or ['redux-saga'](https://github.com/redux-saga/redux-saga). They all come as middleware for the *dispatch()* function and help you deal with such cases. 354 | 355 | 356 | 1. As React apps are rendered on the client side, there are initially no URLs to get around in the app. Nevertheless it makes absolute sense to have URLs and to link to certain references in your app. This is achieved with a package called ['react-router'](https://github.com/ReactTraining/react-router). I am referring to ≥v4 of 'react-router', since quite some API updates have taken place in this version compared to previous versions. Basically 'react-router' offers a Router component, that comes along with a lot of features to manage the routing in your app. If you want to have routing functionality in your app, it is conceptually easiest to just wrap the Router component around you app. 357 | 358 | 359 | ```JSX 360 | import { Provider } from 'react-redux' 361 | import { BrowserRouter as Router } from 'react-router-dom' 362 | 363 | const Root = ( {store} ) => ( 364 | 365 | 366 | 367 | 368 | 369 | ) 370 | ``` 371 | 372 | 1. In your App component you can then use linking and routing by help of NavLink and Route components: 373 | 374 | 375 | ```JSX 376 | import { Provider } from 'react-redux' 377 | import { NavLink, Route } from 'react-router-dom' 378 | 379 | const App = () => ( 380 |
381 | Home 382 | About 383 | Users 384 | 385 | 386 | 387 | 388 |
389 | ) 390 | ``` 391 | 392 | ## Wrapping up 393 | 394 | 1. With all this said you can now perfectly understand the workflow cycle of a React Redux app. We started the explanation with the store, but let's see what happens, when we actually start in the app. In the app a user activity generates an event. The event handler calls the *dispatch()* function that is sending the current state and an action (object) to the *rootReducer()*. The action object contains the relevant data for the requested change of state slice. The *rootReducer()* will interpret the *action.type*, process the data and generate a new state. After the store has received the new state, it triggers the re-render of the React Redux app. It also triggers the execution of all listener functions that are registered with the *subscribe()* method to the store. Furthermore, all components that are subscribed with *connect(mapStateToProps)* to the store now receive the new state data as defined in *mapStateToProps()*. 395 | 396 | 2. I hope this walk-through has added a little bit to understanding Redux and motivates you to now use it. 397 | 398 | 399 | --------------------------------------------------------------------------------