├── .gitignore
├── LICENSE
├── README.md
├── _config.yml
├── fiber
└── book
│ └── Intro.md
└── stack
├── book
├── Intro.md
├── Part-0.md
├── Part-1.md
├── Part-10.md
├── Part-11.md
├── Part-12.md
├── Part-13.md
├── Part-14.md
├── Part-2.md
├── Part-3.md
├── Part-4.md
├── Part-5.md
├── Part-6.md
├── Part-7.md
├── Part-8.md
└── Part-9.md
├── images
├── 0
│ ├── jsx-to-vdom.svg
│ ├── mounting-scheme-1-big.svg
│ ├── mounting-scheme-1-small.svg
│ ├── part-0-A.svg
│ ├── part-0-B.svg
│ ├── part-0-C.svg
│ └── part-0.svg
├── 1
│ ├── communication-channel.svg
│ ├── part-1-A.svg
│ ├── part-1-B.svg
│ ├── part-1-C.svg
│ ├── part-1.svg
│ └── transaction.svg
├── 2
│ ├── part-2-A.svg
│ ├── part-2-B.svg
│ ├── part-2-C.svg
│ └── part-2.svg
├── 3
│ ├── part-3-A.svg
│ ├── part-3-B.svg
│ ├── part-3-C.svg
│ └── part-3.svg
├── 4
│ ├── part-4-A.svg
│ ├── part-4-B.svg
│ ├── part-4-C.svg
│ └── part-4.svg
├── 5
│ ├── part-5-A.svg
│ ├── part-5-B.svg
│ ├── part-5-C.svg
│ └── part-5.svg
├── 6
│ ├── overall-mounting-scheme.svg
│ ├── part-6-A.svg
│ ├── part-6-B.svg
│ ├── part-6-C.svg
│ └── part-6.svg
├── 7
│ ├── mounting-parts-C.jpg
│ ├── mounting-parts-C.svg
│ ├── part-7-A.svg
│ ├── part-7-B.svg
│ ├── part-7-C.svg
│ └── part-7.svg
├── 8
│ ├── part-8-A.svg
│ ├── part-8-B.svg
│ ├── part-8-C.svg
│ └── part-8.svg
├── 9
│ ├── part-9-A.svg
│ ├── part-9-B.svg
│ ├── part-9-C.svg
│ ├── part-9.svg
│ └── set-state-update-start.svg
├── 10
│ ├── part-10-A.svg
│ ├── part-10-B.svg
│ ├── part-10-C.svg
│ └── part-10.svg
├── 11
│ ├── part-11-A.svg
│ ├── part-11-B.svg
│ ├── part-11-C.svg
│ └── part-11.svg
├── 12
│ ├── part-12-A.svg
│ ├── part-12-B.svg
│ ├── part-12-C.svg
│ └── part-12.svg
├── 13
│ ├── part-13-A.svg
│ ├── part-13-B.svg
│ ├── part-13-C.svg
│ └── part-13.svg
├── 14
│ ├── children-update.svg
│ ├── part-14-A.svg
│ ├── part-14-B.svg
│ ├── part-14-C.svg
│ ├── part-14.svg
│ ├── updating-parts-C.jpg
│ └── updating-parts-C.svg
├── intro
│ ├── all-page-stack-reconciler-25-scale.jpg
│ ├── all-page-stack-reconciler.svg
│ ├── files-scheme.svg
│ ├── modules-per-platform-scheme.svg
│ └── modules-src-path.svg
├── mounting-c-parts
│ ├── C-0-mounting-parts.svg
│ ├── C-1-mounting-parts.svg
│ ├── C-2-mounting-parts.svg
│ ├── C-3-mounting-parts.svg
│ ├── C-4-mounting-parts.svg
│ ├── C-5-mounting-parts.svg
│ ├── C-6-mounting-parts.svg
│ └── C-7-mounting-parts.svg
└── updating-c-parts
│ ├── C-10-updating-parts.svg
│ ├── C-11-updating-parts.svg
│ ├── C-12-updating-parts.svg
│ ├── C-13-updating-parts.svg
│ ├── C-14-updating-parts.svg
│ ├── C-8-updating-parts.svg
│ └── C-9-updating-parts.svg
├── languages
├── chinese
│ └── book
│ │ ├── Intro.md
│ │ ├── Part-0.md
│ │ ├── Part-1.md
│ │ ├── Part-10.md
│ │ ├── Part-11.md
│ │ ├── Part-12.md
│ │ ├── Part-13.md
│ │ ├── Part-14.md
│ │ ├── Part-2.md
│ │ ├── Part-3.md
│ │ ├── Part-4.md
│ │ ├── Part-5.md
│ │ ├── Part-6.md
│ │ ├── Part-7.md
│ │ ├── Part-8.md
│ │ ├── Part-9.md
│ │ └── README.md
└── korean
│ └── book
│ ├── Intro.md
│ ├── Part-0.md
│ ├── Part-1.md
│ ├── Part-10.md
│ ├── Part-11.md
│ ├── Part-12.md
│ ├── Part-13.md
│ ├── Part-14.md
│ ├── Part-2.md
│ ├── Part-3.md
│ ├── Part-4.md
│ ├── Part-5.md
│ ├── Part-6.md
│ ├── Part-7.md
│ ├── Part-8.md
│ ├── Part-9.md
│ └── README.md
└── presentation
├── css
└── main.css
├── index.html
├── js
└── app.js
├── lib
├── anime.min.js
├── code-style
│ ├── index.html
│ ├── prism.css
│ └── prism.js
└── impress.js
└── steps.js
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | .idea
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Bohdan Liashenko
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Under the hood: React
2 | This repository contains an explanation of inner work of React. In fact, I was debugging through the entire code base and put all the logic on visual block-schemes, analyzed them, summarized and explained main concepts and approaches. I've already finished with Stack version and now I work with the next, Fiber version.
3 |
4 |
5 | ### I wanted to automate process of "learning and documenting" a complex codebase as much as possible, so I started [Codecrumbs project](https://codecrumbs.io/). It will help to build projects like "Under the hood ReactJs" in a shorter time and in a simpler way!
6 |
7 |
8 |
9 | Each scheme is clickable and can be opened in a new tab, use that to zoom it and be able to read from it. Keep the article and a scheme you are reading about at that moment in separate windows (tabs), that will help to match text and code flow easier.
10 |
11 | We are gonna talk here about both React versions, current one with Stack reconciler and the next one with Fiber (as you probably know, the next version of React will be released soon), so, you can understand better how current React works and appreciate huge achievements on React-Fiber. We use [React v15.4.2](https://github.com/facebook/react/tree/v15.4.2) for explaining how ‘legacy React’ works and React v16.*.*** for ‘Fiber’. Let’s start from old (I have fun to say that) stack version.
12 |
13 |
14 | ## Stack reconciler
15 | [](./stack/images/intro/all-page-stack-reconciler.svg)
16 |
17 | The entire scheme is divided into 15 parts, let's get started.
18 |
19 | > Read in the best format from [github-pages website](https://bogdan-lyashenko.github.io/Under-the-hood-ReactJS/).
20 |
21 | * [Intro](./stack/book/Intro.md)
22 | * [Part 0](./stack/book/Part-0.md)
23 | * [Part 1](./stack/book/Part-1.md)
24 | * [Part 2](./stack/book/Part-2.md)
25 | * [Part 3](./stack/book/Part-3.md)
26 | * [Part 4](./stack/book/Part-4.md)
27 | * [Part 5](./stack/book/Part-5.md)
28 | * [Part 6](./stack/book/Part-6.md)
29 | * [Part 7](./stack/book/Part-7.md)
30 | * [Part 8](./stack/book/Part-8.md)
31 | * [Part 9](./stack/book/Part-9.md)
32 | * [Part 10](./stack/book/Part-10.md)
33 | * [Part 11](./stack/book/Part-11.md)
34 | * [Part 12](./stack/book/Part-12.md)
35 | * [Part 13](./stack/book/Part-13.md)
36 | * [Part 14](./stack/book/Part-14.md)
37 |
38 |
39 |
40 | ## Fiber
41 | Will be done with a help of [js-code-to-svg-flowchart tool](https://github.com/Bogdan-Lyashenko/js-code-to-svg-flowchart) later.
42 | 1. [Intro](./fiber/book/Intro.md) [TODO]
43 |
44 |
45 |
46 | ## Languages
47 | - [Chinese](./stack/languages/chinese/book/Intro.md)
48 | - [Korean](./stack/languages/korean/book/Intro.md)
49 |
--------------------------------------------------------------------------------
/_config.yml:
--------------------------------------------------------------------------------
1 | theme: jekyll-theme-cayman
--------------------------------------------------------------------------------
/fiber/book/Intro.md:
--------------------------------------------------------------------------------
1 | #Fiber..
2 | Still in progress..
3 | I work on big scheme right now..
4 |
--------------------------------------------------------------------------------
/stack/book/Part-10.md:
--------------------------------------------------------------------------------
1 | ## Part 10
2 |
3 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/10/part-10.svg)
4 |
5 | 10.0 Part 10 (clickable)
6 |
7 | ### Dirty components
8 |
9 | As you can see, React loop through `dirtyComponents`(1), and call `ReactUpdates.runBatchedUpdates`(2), through transaction! Transaction? The new one, but why? Let’s see.
10 |
11 | The transaction type is `ReactUpdatesFlushTransaction` and, we’ve already mentioned that before, we need to check `wrappers` to understand what the transaction actually does. A small hint from the code comment:
12 | > ‘ReactUpdatesFlushTransaction's wrappers will clear the dirtyComponents array and perform any updates enqueued by mount-ready handlers (i.e., componentDidUpdate)’
13 |
14 | But, anyway, we need to prove that. There are two wrappers `NESTED_UPDATES` and `UPDATE_QUEUEING`. On `initialize` phase we store `dirtyComponentsLength` (3) and, as you can check on `close`, React compares, maybe during updates a flush number of dirty components was changed, so, obviously it’s needed to run `flushBatchedUpdates` one more time. You see, no magic, everything is pretty straightforward.
15 |
16 | Well.. one magic moment actually is present. `ReactUpdatesFlushTransaction` overrides `Transaction.perform` method, because… it actually requires behavior from `ReactReconcileTransaction` (transaction is used during mounting and allows to keep app state safe). So, inside `ReactUpdatesFlushTransaction.perform` method, `ReactReconcileTransaction` is used as well, so transaction method actually is wrapped one more time.
17 |
18 | So, technically, it looks like:
19 |
20 | ```javascript
21 | [NESTED_UPDATES, UPDATE_QUEUEING].initialize()
22 | [SELECTION_RESTORATION, EVENT_SUPPRESSION, ON_DOM_READY_QUEUEING].initialize()
23 |
24 | method -> ReactUpdates.runBatchedUpdates
25 |
26 | [SELECTION_RESTORATION, EVENT_SUPPRESSION, ON_DOM_READY_QUEUEING].close()
27 | [NESTED_UPDATES, UPDATE_QUEUEING].close()
28 | ```
29 |
30 | We will go back to the transaction in the end, to double check how it helps to finish method work, but now, let’s see details of `ReactUpdates.runBatchedUpdates`(2) (`\src\renderers\shared\stack\reconciler\ReactUpdates.js#125`)
31 |
32 | The first thing we should do from the very beginning - sort `dirtyComponets` array (4). How to sort? By `mount order` (integer number was set to a component when instance mounted), it means that parents (they were mounted first) will be updated first, children next, and so on.
33 | The next step, we increase `updateBatchNumber`, it’s something like ID for current reconciling. According to comment in the code:
34 | > ‘Any updates enqueued while reconciling must be performed after this entire batch. Otherwise, if dirtyComponents is [A, B] where A has children B and C, B could update twice in a single batch if C's render enqueues an update to B (since B would have already updated, we should skip it, and the only way we can know to do so is by checking the batch counter).’
35 |
36 | It helps actually to avoid double updates for the same components.
37 |
38 | Well done, finally we loop through `dirtyComponents` and pass each component to `ReactReconciler.performUpdateIfNecessary` (5), where actually `performUpdateIfNecessary` method will be called from `ReactCompositeComponent` instance, so, move on to `ReactCompositeComponent` code again and its method `updateComponent`. Here we can find something interesting for us, so, let’s dive deeper.
39 |
40 | ### Alright, we’ve finished *Part 10*.
41 |
42 | Let’s recap how we got here. Let's look at the scheme one more time, then remove redundant less important pieces, and it becomes this:
43 |
44 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/10/part-10-A.svg)
45 |
46 | 10.1 Part 10 simplified (clickable)
47 |
48 | And we should probably fix spaces and alignment as well:
49 |
50 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/10/part-10-B.svg)
51 |
52 | 10.2 Part 10 simplified & refactored (clickable)
53 |
54 | Nice. In fact, that’s all that happens here. So, we can take the essential value from *Part 10* and use it for the final `updating` scheme:
55 |
56 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/10/part-10-C.svg)
57 |
58 | 10.3 Part 10 essential value (clickable)
59 |
60 | And then we're done!
61 |
62 |
63 | [To the next page: Part 11 >>](./Part-11.md)
64 |
65 | [<< To the previous page: Part 9](./Part-9.md)
66 |
67 |
68 | [Home](../../README.md)
69 |
--------------------------------------------------------------------------------
/stack/book/Part-11.md:
--------------------------------------------------------------------------------
1 | ## Part 11
2 |
3 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/11/part-11.svg)
4 |
5 | 11.0 Part 11 (clickable)
6 |
7 | ### Update component
8 |
9 | Comment in the code which describes the method says:
10 | >‘Perform an update to a mounted component. The componentWillReceiveProps and shouldComponentUpdate methods are called, then (assuming the update isn't skipped) the remaining update lifecycle methods are called and the DOM representation is updated. By default, this implements React's rendering and reconciliation algorithm. Sophisticated clients may wish to override this.’
11 |
12 | Alright… sounds reasonable.
13 |
14 | The first thing we check if `props` (1) were changed, technically, the method `updateComponent` can be called in two different scenarios if `setState` was called or `props` were changed. If `props` were actually changed, then life-cycle method `componentWillReceiveProps` will be called. After, React re-calculate `nextState` (2) based on `pending state queue` (queue of partial state objects which we set before, in our case queue will be like [{message: "click state message"}]). Of course, in the case with just `props` update state will be untouched.
15 |
16 | Well, next step, we set `shouldUpdate` to default value `true`(3). That’s actually why when `shouldComponentUpdate` is not specified, a component is updated by default. Then, check if it’s not `force update`. As you know, it’s possible to call `forceUpdate` from component to update it, instead of changing `state` or `props`, but, according to React official docs, using this method is bad practice. So, in a case of force update component will be updated permanently, otherwise, specified from the component method `shouldComponentUpdate` will be called, and `shouldUpdate` will be re-assigned with its result value. If it's determined that a component should not update, React still needs to set `props` and `state` but shortcut the rest of the update.
17 |
18 | ### Alright, we’ve finished *Part 11*.
19 |
20 | Let’s recap how we got here. Let's look at the scheme one more time, then remove redundant less important pieces, and it becomes this:
21 |
22 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/11/part-11-A.svg)
23 |
24 | 11.1 Part 11 simplified (clickable)
25 |
26 | And we should probably fix spaces and alignment as well:
27 |
28 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/11/part-11-B.svg)
29 |
30 | 11.2 Part 11 simplified & refactored (clickable)
31 |
32 | Nice. In fact, that’s all that happens here. So, we can take the essential value from *Part 11* and use it for the final `updating` scheme:
33 |
34 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/11/part-11-C.svg)
35 |
36 | 11.3 Part 11 essential value (clickable)
37 |
38 | And then we're done!
39 |
40 |
41 | [To the next page: Part 12 >>](./Part-12.md)
42 |
43 | [<< To the previous page: Part 10](./Part-10.md)
44 |
45 |
46 | [Home](../../README.md)
47 |
--------------------------------------------------------------------------------
/stack/book/Part-12.md:
--------------------------------------------------------------------------------
1 | ## Part 12
2 |
3 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/12/part-12.svg)
4 |
5 | 12.0 Part 12 (clickable)
6 |
7 | ### If components actually should update..
8 |
9 | So, it’s the very beginning of the update, it means it’s a good place to call `componentWillUpdate` hook if it’s specified (1). Then, re-render component and enqueue the call of one more well-known method `componentDidUpdate` (postpone the call, because it should be called in the very end of the update).
10 | What about re-render? Actually, what we need to do here it’s to call the component's `render` method and update the DOM accordingly. So, the first step, we call `render`(2) method from our instance (`ExampleApplication`) and store the result of render (React elements which were returned from a method call). Then, we compare it previous rendered element and see, if DOM actually should be updated.
11 |
12 | You see this, right, it’s actually one of React’s killer features, it avoids redundant DOM updates, what makes React performance really good.
13 | Due to the code comment `shouldUpdateReactComponent`(3) method:
14 | > ‘determines if the existing instance should be updated as opposed to being destroyed or replaced by a new instance’.
15 |
16 | So, roughly speaking, the method check if element should be replaced completely, it means, old one should be `unmounted` first, then new element (got from `render`) should be mounted and markup, received from the `mount` method, should be placed instead of current element, or, if element can be partially updated. The major reason to replace element completely is a case when a new element is empty (was removed by `render` logic) or its type is different, e.g. it was `div` but now it’s something else. Let’s see the code, it’s simple enough.
17 |
18 | ```javascript
19 | ///src/renderers/shared/shared/shouldUpdateReactComponent.js#25
20 |
21 | function shouldUpdateReactComponent(prevElement, nextElement) {
22 | var prevEmpty = prevElement === null || prevElement === false;
23 | var nextEmpty = nextElement === null || nextElement === false;
24 | if (prevEmpty || nextEmpty) {
25 | return prevEmpty === nextEmpty;
26 | }
27 |
28 | var prevType = typeof prevElement;
29 | var nextType = typeof nextElement;
30 | if (prevType === 'string' || prevType === 'number') {
31 | return (nextType === 'string' || nextType === 'number');
32 | } else {
33 | return (
34 | nextType === 'object' &&
35 | prevElement.type === nextElement.type &&
36 | prevElement.key === nextElement.key
37 | );
38 | }
39 | }
40 | ```
41 |
42 | Alright, in the case with our `ExampleApplication` we just updated `state` property which doesn’t affect `render` so much, so, we go with the second scenario, meaning `update`.
43 |
44 | ### Alright, we’ve finished *Part 12*.
45 |
46 | Let’s recap how we got here. Let's look at the scheme one more time, then remove redundant less important pieces, and it becomes this:
47 |
48 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/12/part-12-A.svg)
49 |
50 | 12.1 Part 12 simplified (clickable)
51 |
52 | And we should probably fix spaces and alignment as well:
53 |
54 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/12/part-12-B.svg)
55 |
56 | 12.2 Part 12 simplified & refactored (clickable)
57 |
58 | Nice. In fact, that’s all that happens here. So, we can take the essential value from *Part 12* and use it for the final `updating` scheme:
59 |
60 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/12/part-12-C.svg)
61 |
62 | 12.3 Part 12 essential value (clickable)
63 |
64 | And then we're done!
65 |
66 |
67 | [To the next page: Part 13 >>](./Part-13.md)
68 |
69 | [<< To the previous page: Part 11](./Part-11.md)
70 |
71 |
72 | [Home](../../README.md)
73 |
--------------------------------------------------------------------------------
/stack/book/Part-13.md:
--------------------------------------------------------------------------------
1 | ## Part 13
2 |
3 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/13/part-13.svg)
4 |
5 | 13.0 Part 13 (clickable)
6 |
7 | ### Receive component (next element, to be more precise)
8 |
9 | Well, through `ReactReconciler.receiveComponent` React actually calls `receiveComponent` from `ReactDOMComponent` and pass next element there. Reassign it on DOM component instance and call update method. `updateComponent` method actually performs two main actions: update DOM properties and DOM children, based on `prev` and `next` props. Good for us, we already analyzed `_updateDOMProperties` (`src\renderers\dom\shared\ReactDOMComponent.js#946`) method. As you remember, this method mostly process HTML elements properties and attributes, compute styles, handle event listeners etc. What is left, it’s `_updateDOMChildren` (`src\renderers\dom\shared\ReactDOMComponent.js#1076`).
10 |
11 | ### Alright, we’ve finished *Part 13*. That was a short one.)
12 |
13 | Let’s recap how we got here. Let's look at the scheme one more time, then remove redundant less important pieces, and it becomes this:
14 |
15 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/13/part-13-A.svg)
16 |
17 | 13.1 Part 13 simplified (clickable)
18 |
19 | And we should probably fix spaces and alignment as well:
20 |
21 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/13/part-13-B.svg)
22 |
23 | 13.2 Part 13 simplified & refactored (clickable)
24 |
25 | Nice. In fact, that’s all that happens here. So, we can take the essential value from *Part 13* and use it for the final `updating` scheme:
26 |
27 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/13/part-13-C.svg)
28 |
29 | 13.3 Part 13 essential value (clickable)
30 |
31 | And then we're done!
32 |
33 |
34 | [To the next page: Part 14 >>](./Part-14.md)
35 |
36 | [<< To the previous page: Part 12](./Part-12.md)
37 |
38 |
39 | [Home](../../README.md)
40 |
--------------------------------------------------------------------------------
/stack/book/Part-2.md:
--------------------------------------------------------------------------------
1 | ## Part 2
2 |
3 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/2/part-2.svg)
4 |
5 | 2.0 Part 2 (clickable)
6 |
7 | ### One more transaction
8 |
9 | This time it’s `ReactReconcileTransaction`. As you already know, the major thing that is interesting to us is transaction wrappers. There are three wrappers:
10 |
11 | ```javascript
12 | //\src\renderers\dom\client\ReactReconcileTransaction.js#89
13 | var TRANSACTION_WRAPPERS = [
14 | SELECTION_RESTORATION,
15 | EVENT_SUPPRESSION,
16 | ON_DOM_READY_QUEUEING,
17 | ];
18 | ```
19 |
20 | As we can see these wrappers are used mostly to **keep the actual state**, lock some changeable values before method calls, and release them after. So, React ensures that, for example, the selection range (currently selected text input) is not disturbed by performing the transaction (get selected on `initialize` and restore on `close`). Also, it suppresses events (blur/focus) that could be inadvertently dispatched due to high-level DOM manipulations (like temporarily removing a text input from the DOM) so it **disables `ReactBrowserEventEmitter`** on `initialize` and enables on `close`.
21 |
22 | Well, we are really close to starting the component mount, which will return us markup ready to put into DOM. Actually, `ReactReconciler.mountComponent` is just wrapper, or, it's more correct to say ‘mediator’. It delegates method mounting to component modules. This is an important moment, so let’s highlight:
23 |
24 | > `ReactReconciler` module is always called in cases when implementation of some logic **depends on platform**, like this exact case. Mount is different per platform, so the ‘main module’ talks to `ReactReconciler` and `ReactReconciler` knows what to do next.
25 |
26 | Alright, let's move on to the component’s method `mountComponent`. It's probably the method you have already heard about. It initializes the component, renders markup, and registers event listeners. You see, a long way through and we finally see a component mounting call. After calling mount, we should get actual HTML elements which can be put into the document.
27 |
28 |
29 | ### Alright, we’ve finished *Part 2*.
30 |
31 | Let’s recap how we got here. Let's look at the scheme one more time, then remove redundant less important pieces, and it becomes this:
32 |
33 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/2/part-2-A.svg)
34 |
35 | 2.1 Part 2 simplified (clickable)
36 |
37 | And we should probably fix spaces and alignment as well:
38 |
39 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/2/part-2-B.svg)
40 |
41 | 2.2 Part 2 simplified & refactored (clickable)
42 |
43 | Nice. In fact, that’s all that happens here. So, we can take the essential value from *Part 1* and use it for the final `mounting` scheme:
44 |
45 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/2/part-2-C.svg)
46 |
47 | 2.3 Part 2 essential value (clickable)
48 |
49 | And then we're done!
50 |
51 |
52 | [To the next page: Part 3 >>](./Part-3.md)
53 |
54 | [<< To the previous page: Part 1](./Part-1.md)
55 |
56 |
57 | [Home](../../README.md)
58 |
--------------------------------------------------------------------------------
/stack/book/Part-4.md:
--------------------------------------------------------------------------------
1 | ## Part 4
2 |
3 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/4/part-4.svg)
4 |
5 | 4.0 Part 4 (clickable)
6 |
7 | ### Child mounting
8 |
9 | Going crazy, right? Let’s continue with investigating the `mount` method.
10 |
11 | So, if `_tag` contains a ‘complex’ tag (1), like video, form, textarea, etc., it will require additional wrapping. It adds more event listeners for each media event, like ‘volumechange’ for `audio` tags, or it just wraps native behavior of tags like `select`, `textarea`, etc.
12 | There are a bunch of wrappers for elements like that, such as `ReactDOMSelect` and `ReactDOMTextarea` (inside src\renderers\dom\client\wrappers\ folder). In our case it’s just simply `div`, no additional processing there.
13 |
14 | ### Props validation
15 |
16 | The next validation method is called just to make sure that the internal `props` are set correctly, otherwise it will throw errors. For example, if `props.dangerouslySetInnerHTML` is set (usually we do that when try to insert HTML from a string) and object key `__html` is missed, the next error will be thrown:
17 |
18 | > `props.dangerouslySetInnerHTML` must be in the form `{__html: ...}`. Please visit https://fb.me/react-invariant-dangerously-set-inner-html for more information.
19 |
20 | ### Create HTML element
21 |
22 | Then, the actual HTML element will be created (3) by `document.createElement`, which will instantiate the real HTML `div` for us. Before we worked only with virtual representation and now, you can see it for the first time.
23 |
24 |
25 | ### Alright, we’ve finished *Part 4*.
26 |
27 | Let’s recap how we got here. Let's look at the scheme one more time, then remove redundant less important pieces, and it becomes this:
28 |
29 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/4/part-4-A.svg)
30 |
31 | 4.1 Part 4 simplified (clickable)
32 |
33 | And we should probably fix spaces and alignment as well:
34 |
35 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/4/part-4-B.svg)
36 |
37 | 4.2 Part 4 simplified & refactored (clickable)
38 |
39 | Nice. In fact, that’s all that happens here. So, we can take the essential value from *Part 4* and use it for the final `mounting` scheme:
40 |
41 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/4/part-4-C.svg)
42 |
43 | 4.3 Part 4 essential value (clickable)
44 |
45 | And then we're done!
46 |
47 |
48 | [To the next page: Part 5 >>](./Part-5.md)
49 |
50 | [<< To the previous page: Part 3](./Part-3.md)
51 |
52 |
53 | [Home](../../README.md)
54 |
--------------------------------------------------------------------------------
/stack/book/Part-5.md:
--------------------------------------------------------------------------------
1 | ## Part 5
2 |
3 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/5/part-5.svg)
4 |
5 | 5.0 Part 5 (clickable)
6 |
7 | ### Update DOM properties
8 |
9 | Alright, that looks scary, right? The main idea is to apply the diff of previous and new `props` efficiently. Look at the method comment in the code:
10 | > “Reconciles the properties by detecting differences in property values and updating the DOM as necessary. This function is probably the single most critical path for performance optimization.”
11 |
12 | There are two loops actually. First, through previous `props` and then, through next `props`. In our case, with mounting, `lastProps` (previous) is empty (obviously, it’s the first time when we assign props), but still, let see what’s going on here.
13 |
14 | ### Last `props` loop
15 | In the first step, we check if `nextProps` contains the same prop value. If so, we just skip it, because it will be handled later in the `nextProps` loop. Then, we reset style values, delete event listeners (if they were set before), and remove DOM attribute and DOM properties values. For attributes, we make sure they are not one of the `RESERVED_PROPS`, that it's actually `prop`, like `children` or `dangerouslySetInnerHTML`.
16 |
17 | ### Next `props` loop
18 | Here, the first step is to check if `prop` was changed, meaning if the next value is different than the old one. If not, we don’t do anything. For `styles`, (you might have noticed it’s treated a bit special) we update values that have changed since `lastProp`. Then, we add the events listeners (yes, exactly the ones like `onClick`, etc). Let’s analyze that with more details.
19 |
20 | The important thing is, across the React app, all work is passed through named ‘synthetic’ events. Nothing is special, it's just a few more wrappers for more efficient work. Next thing, the mediator module for managing event listeners is `EventPluginHub` (`src\renderers\shared\stack\event\EventPluginHub.js`). It contains a `listenerBank` map for caching and managing all listeners.
21 | We are going to add our event listeners, but not right away. The point is, that we should add listeners when the component and DOM element is ready for handling events. Seems like we have delayed execution here, but you will probably ask, how can we know when that moment happens? Well, it’s time for the next answer! Do you remember when we passed `transaction` through all of the methods and calls? Exactly! We did that because it can be helpful exactly for such a situation. Let’s see the proof in the code:
22 |
23 | ```javascript
24 | //src\renderers\dom\shared\ReactDOMComponent.js#222
25 | transaction.getReactMountReady().enqueue(putListener, {
26 | inst: inst,
27 | registrationName: registrationName,
28 | listener: listener,
29 | });
30 | ```
31 |
32 | Okay, after the event listeners, we set DOM attribute and DOM property values. Same as before, for attributes we make sure they are not one of the `RESERVED_PROPS`, that it’s actually `prop`, like `children` or `dangerouslySetInnerHTML`.
33 |
34 | During the processing of the last and next props, we computed the `styleUpdates` config and now pass it to the `CSSPropertyOperations` module.
35 |
36 | Well, we’ve finished updating the properties. Let’s move on.
37 |
38 | ### Alright, we’ve finished *Part 5*.
39 |
40 | Let’s recap how we got here. Let's look at the scheme one more time, then remove redundant less important pieces, and it becomes this:
41 |
42 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/5/part-5-A.svg)
43 |
44 | 5.1 Part 5 simplified (clickable)
45 |
46 | And we should probably fix spaces and alignment as well:
47 |
48 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/5/part-5-B.svg)
49 |
50 | 5.2 Part 5 simplified & refactored (clickable)
51 |
52 | Nice. In fact, that’s all that happens here. So, we can take the essential value from *Part 5* and use it for the final `mounting` scheme:
53 |
54 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/5/part-5-C.svg)
55 |
56 | 5.3 Part 5 essential value (clickable)
57 |
58 | And then we're done!
59 |
60 |
61 | [To the next page: Part 6 >>](./Part-6.md)
62 |
63 | [<< To the previous page: Part 4](./Part-4.md)
64 |
65 |
66 | [Home](../../README.md)
67 |
--------------------------------------------------------------------------------
/stack/book/Part-6.md:
--------------------------------------------------------------------------------
1 | ## Part 6
2 |
3 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/6/part-6.svg)
4 |
5 | 6.0 Part 6 (clickable)
6 |
7 | ### Create initial children
8 |
9 | Seems like the element itself is finished, so now we can continue with its children. Two steps here: children should be mounted (`this.mountChildren`)(1) and connected to the parent (`DOMLazyTree.queueChild`)(2). Let’s move to children mounting because it’s obviously more interesting.
10 |
11 | There is a separate module called `ReactMultiChild` (`src\renderers\shared\stack\reconciler\ReactMultiChild.js`) to manage children. Nice, let’s check the `mountChildren` method then. It contains two main tasks as well. First of all, we instantiate children (use `ReactChildReconciler` for that) and mount them. What children are actually here? It can be a simple HTML tag or another custom component. To handle HTML we need to instantiate `ReactDOMComponent` and for custom component - `ReactCompositeComponent`. The mounting flow, again, depends on what the child type is.
12 |
13 | ### One more time
14 |
15 | If you are still reading this, it's probably time to clarify and review the overall process one more time. Let’s take a break and recollect the sequence of objects.
16 |
17 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/6/overall-mounting-scheme.svg)
18 |
19 | 6.1 Overall mounting scheme (clickable)
20 |
21 | 1) React instantiates `ReactCompositeComponent` for each of your custom components (with component lifecycle hooks like `componentWillMount`, etc) and mounts it.
22 |
23 | 2) During mounting, at first, an instance of your custom component will be created (`constructor` called).
24 |
25 | 3) Then, its render method is called (for a simple example, render returns `div`) and `React.createElement` creates the React elements. It can be called directly or after parsing JSX by Babel and replacing tags in your render. But, it’s not exactly what we need, see what's next below.
26 |
27 | 4) We need a DOM component for our `div`. So, during an instantiation process, we create instances of `ReactDOMComponent` from the element-objects (mentioned above).
28 |
29 | 5) Then, we need to mount the DOM component. That actually means we create the DOM elements and assign event listeners, etc.
30 |
31 | 6) Then, we process the initial children of our DOM component. We create instances of them and mount them as well. Depending on what each item of the children is, a custom component or just an HTML tag, we recurse to step 1) or step 5) respectively. And then again for all nested elements.
32 |
33 | That’s it. It's pretty straightforward as you can see.
34 |
35 | So, mounting is basically finished. Enqueue the `componentDidMount` method! Great job.
36 |
37 | ### Alright, we’ve finished *Part 6*.
38 |
39 | Let’s recap how we got here. Let's look at the scheme one more time, then remove redundant less important pieces, and it becomes this:
40 |
41 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/6/part-6-A.svg)
42 |
43 | 6.2 Part 6 simplified (clickable)
44 |
45 | And we should probably fix spaces and alignment as well:
46 |
47 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/6/part-6-B.svg)
48 |
49 | 6.3 Part 6 simplified & refactored (clickable)
50 |
51 | Nice. In fact, that’s all that happens here. So, we can take the essential value from *Part 6* and use it for the final `mounting` scheme:
52 |
53 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/6/part-6-C.svg)
54 |
55 | 6.4 Part 6 essential value (clickable)
56 |
57 | And then we're done!
58 |
59 |
60 | [To the next page: Part 7 >>](./Part-7.md)
61 |
62 | [<< To the previous page: Part 5](./Part-5.md)
63 |
64 |
65 | [Home](../../README.md)
66 |
--------------------------------------------------------------------------------
/stack/book/Part-7.md:
--------------------------------------------------------------------------------
1 | ## Part 7
2 |
3 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/7/part-7.svg)
4 |
5 | 7.0 Part 7 (clickable)
6 |
7 | ### Back to the beginning
8 |
9 | After mounting as result of method execution, we have HTML elements which are ready to be set into a document. Actually, `markup` (1) is generated, but `mountComponent`, despite how it’s named, is not actually HTML markup. It’s a data structure with fields `children`, `node` (actual DOM nodes), etc. But, we have our HTML element to put into the container (the one specified as the container in the `ReactDOM.render` call). While adding it into DOM, React will erase everything that was there before. `DOMLazyTree`(2) is a utils class that performs some operations with tree data structures, which we're actually doing during work with the DOM.
10 |
11 | The last thing is `parentNode.insertBefore(tree.node)`(3), where `parentNode` is the container `div` node and `tree.node` is actually our `ExampleAppliication` div node. Nice, the HTML elements that were created during mounting were finally inserted into the document.
12 |
13 | So, that’s it? Not exactly. As you remember, the `mount` call was wrapped into a transaction. It means that we should close it. Let’s check our `close` wrappers list. Mostly, we should restore some locked behavior `ReactInputSelection.restoreSelection()`, `ReactBrowserEventEmitter.setEnabled(previouslyEnabled)`, but also, we will notify all of the callbacks `this.reactMountReady.notifyAll`(4) we put into `transaction.reactMountReady` queue before. One of them is our favorite `componentDidMount`, which will be triggered exactly by the `close` wrapper.
14 |
15 | Now you have a clear picture of what ‘component did mount’ actually means. Cheers!
16 |
17 | ### One more transaction to close
18 |
19 | Well, actually, that transaction was not only one. We forgot one more which was used to wrap the `ReactMount.batchedMountComponentIntoNode` call. Let’s close it as well.
20 |
21 | Here, we check the wrapper `ReactUpdates.flushBatchedUpdates`(5), which will process `dirtyComponents`. Sounds interesting, ya? Well, it's good or bad news. We just did our first mount, so there are no dirty components yet. It means that it’s an idle call. So, we can close this transaction as well and say that the batching strategy updates are done.
22 |
23 | ### Alright, we’ve finished *Part 7*.
24 |
25 | Let’s recap how we got here. Let's look at the scheme one more time, then remove redundant less important pieces, and it becomes this:
26 |
27 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/7/part-7-A.svg)
28 |
29 | 7.1 Part 7 simplified (clickable)
30 |
31 | And we should probably fix spaces and alignment as well:
32 |
33 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/7/part-7-B.svg)
34 |
35 | 7.2 Part 7 simplified & refactored (clickable)
36 |
37 | Nice. In fact, that’s all that happens here. So, we can take the essential value from *Part 7* and use it for the final `mounting` scheme:
38 |
39 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/7/part-7-C.svg)
40 |
41 | 7.3 Part 7 essential value (clickable)
42 |
43 | And then we're done! In fact, we're done with mounting. Let's see it below!
44 |
45 |
46 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/7/mounting-parts-C.svg)
47 |
48 | 7.4 Mounting (clickable)
49 |
50 | [To the next page: Part 8 >>](./Part-8.md)
51 |
52 | [<< To the previous page: Part 6](./Part-6.md)
53 |
54 |
55 | [Home](../../README.md)
56 |
--------------------------------------------------------------------------------
/stack/book/Part-8.md:
--------------------------------------------------------------------------------
1 | ## Part 8
2 |
3 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/8/part-8.svg)
4 |
5 | 8.0 Part 8 (clickable)
6 |
7 | ### `this.setState`
8 |
9 | We know how mounting works, but now, let’s go from the another side. Yes, `setState` method, one more piece of cake!
10 |
11 | First of all, why we actually can call some method called `setState`? Well, that’s clear enough, we inherited our component from `ReactComponent`. Alright, then, it’s easy to find this class in React source and check its `setState` method.
12 |
13 | ```javascript
14 | //src\isomorphic\modern\class\ReactComponent.js#68
15 | this.updater.enqueueSetState(this, partialState)
16 | ```
17 | As you can see, there is some `updater` interface. What that `updater` is? Well, if you check mounting process we’ve just analyzed, during `mountComponent`, instance receives `updater` property as a reference to `ReactUpdateQueue` (`src\renderers\shared\stack\reconciler\ReactUpdateQueue.js`).
18 |
19 | Well, dive inside `enqueueSetState` (1) method and see that, at first, it pushes partial state (a partial state is an object you pass into `this.setState`) to `_pendingStateQueue` (2) of internal instance (just to remind: public instance, it’s actually our custom component `ExampleApplication` and, internal instance is `ReactCompositeComponent` which was created during mounting), secondary, we `enqueueUpdate`, what actually check if updates already in progress and push our component to `dirtyComponents` list, otherwise, if not - init update transaction and then push component to `dirtyComponents` list.
20 |
21 | To summarize this, each component has own list of pending states, means, each time you call `setState` in one transaction, you just push that objects into a queue, then, later, they will be merged into component state one by one. And, when you call `setState`, you add your component into `dirtyComponents` list. Probably, you are already wondering, how that `dirtyComponents` are processed? You are right, that’s next important piece of the puzzle...
22 |
23 | ### Alright, we’ve finished *Part 8*.
24 |
25 | Let’s recap how we got here. Let's look at the scheme one more time, then remove redundant less important pieces, and it becomes this:
26 |
27 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/8/part-8-A.svg)
28 |
29 | 8.1 Part 8 simplified (clickable)
30 |
31 | And we should probably fix spaces and alignment as well:
32 |
33 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/8/part-8-B.svg)
34 |
35 | 8.2 Part 8 simplified & refactored (clickable)
36 |
37 | Nice. In fact, that’s all that happens here. So, we can take the essential value from *Part 8* and use it for the final `updating` scheme:
38 |
39 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/8/part-8-C.svg)
40 |
41 | 8.3 Part 8 essential value (clickable)
42 |
43 | And then we're done!
44 |
45 |
46 | [To the next page: Part 9 >>](./Part-9.md)
47 |
48 | [<< To the previous page: Part 7](./Part-7.md)
49 |
50 |
51 | [Home](../../README.md)
52 |
--------------------------------------------------------------------------------
/stack/book/Part-9.md:
--------------------------------------------------------------------------------
1 | ## Part 9
2 |
3 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/9/part-9.svg)
4 |
5 | 9.0 Part 9 (clickable)
6 |
7 | ### But let’s move back..
8 |
9 | As you noticed on the scheme, the call of `setState` method, can be triggered in several ways, be more precise, with, or without, external impact (means ‘user action’). Let’s take two cases: in the first case, the method call is triggered by mouse click, and second, just call from `setTimeout` in `componentDidMount`.
10 |
11 | What actually makes that difference? Well, as you remember, React process updates in `batches`, it means that list of updates should be somehow collected and, then, `flushed`. The thing is that when mouse event appears, it’s handled on the top level and then, through several layers of wrappers the batched update will be started. By the way, as you can see it happens only if `ReactEventListener` is `enabled` (1), and, if you remember, during a component mounting phase, one of `ReactReconcileTransaction` wrappers actually disables it, and make mounting safe. Smart enough! But, what about `setTimeout` case? It’s also simple, before putting a component into `dirtyComponents` list React will make sure that transaction is started (opened), so then, later, it should be closed and updates flushed.
12 |
13 | As you know, React implements ‘synthetic events’, some ‘syntax sugar’ which in fact wraps native events. But then, later, they still try to behave how we all used to see events. You can see the comment in the code:
14 | > ‘To help development we can get better dev tool integration by simulating a real browser event’
15 |
16 | ```javascript
17 | var fakeNode = document.createElement('react');
18 |
19 | ReactErrorUtils.invokeGuardedCallback = function (name, func, a) {
20 | var boundFunc = func.bind(null, a);
21 | var evtType = 'react-' + name;
22 |
23 | fakeNode.addEventListener(evtType, boundFunc, false);
24 |
25 | var evt = document.createEvent('Event');
26 | evt.initEvent(evtType, false, false);
27 |
28 | fakeNode.dispatchEvent(evt);
29 | fakeNode.removeEventListener(evtType, boundFunc, false);
30 | };
31 | ```
32 | Alright, back to our update, let’s see one more time. The approach is:
33 |
34 | 1. call setState
35 | 1. open batching transaction if it’s not opened yet
36 | 1. add affected components to `dirtyComponents` list,
37 | 1. close transaction with calling `ReactUpdates.flushBatchedUpdates`, what actually means ‘process whatever was collected into `dirtyComponents`’.
38 |
39 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/9/set-state-update-start.svg)
40 |
41 | 9.1 `setState` start (clickable)
42 |
43 | ### Alright, we’ve finished *Part 9*.
44 |
45 | Let’s recap how we got here. Let's look at the scheme one more time, then remove redundant less important pieces, and it becomes this:
46 |
47 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/9/part-9-A.svg)
48 |
49 | 9.2 Part 9 simplified (clickable)
50 |
51 | And we should probably fix spaces and alignment as well:
52 |
53 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/9/part-9-B.svg)
54 |
55 | 9.3 Part 9 simplified & refactored (clickable)
56 |
57 | Nice. In fact, that’s all that happens here. So, we can take the essential value from *Part 9* and use it for the final `updating` scheme:
58 |
59 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/master/stack/images/9/part-9-C.svg)
60 |
61 | 9.6 Part 9 essential value (clickable)
62 |
63 | And then we're done!
64 |
65 |
66 | [To the next page: Part 10 >>](./Part-10.md)
67 |
68 | [<< To the previous page: Part 8](./Part-8.md)
69 |
70 |
71 | [Home](../../README.md)
72 |
--------------------------------------------------------------------------------
/stack/images/0/mounting-scheme-1-small.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/0/part-0-C.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/1/part-1-C.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/10/part-10-C.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/11/part-11-C.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/13/part-13-C.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/14/part-14-C.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/14/updating-parts-C.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/49cf5d7c7db43c02ac1ac17469a072bd97327614/stack/images/14/updating-parts-C.jpg
--------------------------------------------------------------------------------
/stack/images/2/part-2-C.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/4/part-4-C.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/5/part-5-C.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/6/part-6-C.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/7/mounting-parts-C.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/49cf5d7c7db43c02ac1ac17469a072bd97327614/stack/images/7/mounting-parts-C.jpg
--------------------------------------------------------------------------------
/stack/images/8/part-8-C.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/9/part-9-C.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/intro/all-page-stack-reconciler-25-scale.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/49cf5d7c7db43c02ac1ac17469a072bd97327614/stack/images/intro/all-page-stack-reconciler-25-scale.jpg
--------------------------------------------------------------------------------
/stack/images/mounting-c-parts/C-0-mounting-parts.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/mounting-c-parts/C-1-mounting-parts.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/mounting-c-parts/C-2-mounting-parts.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/mounting-c-parts/C-4-mounting-parts.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/mounting-c-parts/C-5-mounting-parts.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/updating-c-parts/C-10-updating-parts.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/updating-c-parts/C-13-updating-parts.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/updating-c-parts/C-14-updating-parts.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/images/updating-c-parts/C-9-updating-parts.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/stack/languages/chinese/book/Intro.md:
--------------------------------------------------------------------------------
1 | ## 介绍
2 |
3 | ### 初识流程图
4 |
5 |
6 | [](https://github.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/raw/master/stack/images/intro/all-page-stack-reconciler-25-scale.svg)
7 |
8 | 图 介绍-0:整体流程
9 |
10 | 你可以先花点时间看下整体的流程。虽然看起来很复杂,但它实际上只描述了两个流程:(组件的)挂载和更新。我跳过了卸载,因为它是一种“反向挂载”,而且删除这部分简化了流程图。另外,**这图并不是100%** 同源代码匹配,而只是描述架构的主要部分。总体来说,它大概是源代码的 60%,而另外的 40% 没有多少视觉价值,为了简单起见,我省略了那部分。
11 |
12 | 乍一看,你可能会注意到流程图中有很多颜色。每个逻辑项(流程图上的形状)都以其父模块的颜色高亮显示。例如,如果是从红色的 `模块 B` 调用 `方法 A`,那 `方法 A` 也是红色的。以下是流程图中模块的图例以及每个文件的路径。
13 |
14 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/7c2372e1/stack/images/intro/modules-src-path.svg)
15 |
16 |
17 |
18 | 让我们把它们放在一张流程图中,看看**模块之间的依赖关系**。
19 |
20 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/7c2372e1/stack/images/intro/files-scheme.svg)
21 |
22 |
23 |
24 | 你可能知道,React 是为**支持多种环境**而构建的。
25 | - 移动端(**ReactNative**)
26 | - 浏览器(**ReactDOM**)
27 | - 服务端渲染
28 | - **ReactART**(使用 React 绘制矢量图形)
29 | - 其它
30 |
31 | 因此,一些文件实际上比上面流程图中列出的要更大。以下是包含多环境支持的相同的流程图。
32 |
33 | [](https://rawgit.com/Bogdan-Lyashenko/Under-the-hood-ReactJS/7c2372e1/stack/images/intro/modules-per-platform-scheme.svg)
34 |
35 |
36 |
37 | 如你所见,有些项似乎翻倍了。这表明它们对每个平台都有一个独立的实现。让我们来看一些简单例子,例如 ReactEventListener,显然,不同平台会有不同的实现。从技术上讲,你可以想象,这些依赖于平台的模块,应该以某种方式注入或连接到当前的逻辑流程中。实际上有很多这样的注入器,因为它们的用法是标准组合模式的一部分。同样,为了简单起见,我选择忽略它们。
38 |
39 | 让我们来学习下**常规浏览器**中 **React DOM** 的逻辑流程。这是最常用的平台,并完全覆盖了所有 React 的架构设计理念。
40 |
41 |
42 | ### 代码示例
43 |
44 | 学习框架或者库的源码的最佳方式是什么?没错,研读并调试源码。那好,我们将要调试这**两个流程**:**ReactDOM.render** 和 **component.setState** 这两者对应了组件的挂载和更新。让我们来看一下我们能编写一些什么样的代码来开始学习。我们需要什么呢?或许几个具有简单渲染的小组件就可以了,因为更容易调试。
45 |
46 | ```javascript
47 | class ChildCmp extends React.Component {
48 | render() {
49 | return