|
| 1 | +// Tutorial 0 - introduction.js |
| 2 | + |
| 3 | +// Why this tutorial? |
| 4 | +// While trying to learn Redux, I realized that I had accumulated incorrect knowledge about flux through |
| 5 | +// articles I read and personal experience. I don't mean that articles about flux are not well written |
| 6 | +// but I just didn't grasp concepts correctly. In the end, I was just applying documentation of different |
| 7 | +// flux frameworks (Reflux, Flummox, FB Flux) and trying to make them match with the theoretical concept I read |
| 8 | +// about (actions / actions creators, store, dispatcher, etc). |
| 9 | +// Only when I started using Redux did I realize that flux is more simple than I thought. This is all |
| 10 | +// thanks to Redux being very well designed and having removed a lot of "anti-boilerplate features" introduced |
| 11 | +// by other frameworks I tried before. I now feel that Redux is a much better way to learn about flux |
| 12 | +// than many other frameworks. That's why I want now to share with everyone, using my own words, |
| 13 | +// flux concepts that I am starting to grasp, focusing on the use of Redux. |
| 14 | + |
| 15 | +// You may have seen this diagram representing the famous unidirectional data flow of a flux application: |
| 16 | + |
| 17 | +/* |
| 18 | + _________ ____________ ___________ |
| 19 | + | | | | | | |
| 20 | + | Action |------------▶| Dispatcher |------------▶| callbacks | |
| 21 | + |_________| |____________| |___________| |
| 22 | + ▲ | |
| 23 | + | | |
| 24 | + | | |
| 25 | + _________ ____|_____ ____▼____ |
| 26 | +| |◀----| Action | | | |
| 27 | +| Web API | | Creators | | Store | |
| 28 | +|_________|----▶|__________| |_________| |
| 29 | + ▲ | |
| 30 | + | | |
| 31 | + ____|________ ____________ ____▼____ |
| 32 | + | User | | React | | Change | |
| 33 | + | interactions |◀--------| Views |◀-------------| events | |
| 34 | + |______________| |___________| |_________| |
| 35 | +
|
| 36 | +*/ |
| 37 | + |
| 38 | +// In this tutorial we'll gradually introduce you to concepts of the diagram above. But instead of trying |
| 39 | +// to explain this complete diagram and the overall flow it describes, we'll take each piece separately and try to |
| 40 | +// understand why it exists and what role it plays. In the end you'll see that this diagram makes perfect sense |
| 41 | +// once we understand each of its parts. |
| 42 | + |
| 43 | +// But before we start, let's talk a little bit about why flux exists and why we need it... |
| 44 | +// Let's pretend we're building a web application. What are all web applications made of? |
| 45 | +// 1) Templates / html = View |
| 46 | +// 2) Data that will populate our views = Models |
| 47 | +// 3) Logic to retrieve data, glue all views together and to react accordingly to user events, |
| 48 | +// data modifications, etc. = Controller |
| 49 | + |
| 50 | +// This is the very classic MVC that we all know about. But it actually looks like concepts of flux, |
| 51 | +// just expressed in a slightly different way: |
| 52 | +// - Models look like stores |
| 53 | +// - user events, data modifications and their handlers look like |
| 54 | +// "action creators" -> action -> dispatcher -> callback |
| 55 | +// - Views look like React views (or anything else as far as flux is concerned) |
| 56 | + |
| 57 | +// So is flux just a matter of new vocabulary? Not exactly. But vocabulary DOES matter, because by introducing |
| 58 | +// these new terms we are now able to express more precisely things that were regrouped under |
| 59 | +// various terminologies... For example, isn't a data fetch an action? Just like a click is also an action? |
| 60 | +// And a change in an input is an action too... Then we're all already used to issuing actions from our |
| 61 | +// applications, we were just calling them differently. And instead of having handlers for those |
| 62 | +// actions directly modify Models or Views, flux ensures all actions go first through something called |
| 63 | +// a dispatcher, then through our stores, and finally all watchers of stores are notified. |
| 64 | + |
| 65 | +// To get more clarity how MVC and flux differ, we'll |
| 66 | +// take a classic use-case in an MVC application: |
| 67 | +// In a classic MVC application you could easily end up with: |
| 68 | +// 1) User clicks on button "A" |
| 69 | +// 2) A click handler on button "A" triggers a change on Model "A" |
| 70 | +// 3) A change handler on Model "A" triggers a change on Model "B" |
| 71 | +// 4) A change handler on Model "B" triggers a change on View "B" that re-renders itself |
| 72 | + |
| 73 | +// Finding the source of a bug in such an environment when something goes wrong can become quite challenging |
| 74 | +// very quickly. This is because every View can watch every Model, and every Model can watch other Models, so |
| 75 | +// basically data can arrive from a lot of places and be changed by a lot of sources (any views or any models). |
| 76 | + |
| 77 | +// Whereas when using flux and its unidirectional data flow, the example above could become: |
| 78 | +// 1) user clicks on button "A" |
| 79 | +// 2) a handler on button "A" triggers an action that is dispatched and produces a change on Store "A" |
| 80 | +// 3) since all other stores are also notified about the action, Store B can react to the same action too |
| 81 | +// 4) View "B" gets notified by the change in Stores A and B, and re-renders |
| 82 | + |
| 83 | +// See how we avoid directly linking Store A to Store B? Each store can only be |
| 84 | +// modified by an action and nothing else. And once all stores have replied to an action, |
| 85 | +// views can finally update. So in the end, data always flows in one way: |
| 86 | +// action -> store -> view -> action -> store -> view -> action -> ... |
| 87 | + |
| 88 | +// Just as we started our use case above from an action, let's start our tutorial with |
| 89 | +// actions and action creators. |
| 90 | + |
| 91 | +// Go to next tutorial: 01_simple-action-creator.js |
0 commit comments