diff --git a/high-performance-react.org b/high-performance-react.org index c375d85..340b577 100644 --- a/high-performance-react.org +++ b/high-performance-react.org @@ -56,7 +56,7 @@ the method will remain the same. TODO note that the book references React-DOM but the algorithms should generally apply to all React implementations. -* Mini React +* Fundamentals: Building our own React Baking bread. When I first began to learn how to bake bread the recipe told me what to do. It listed some ingredients and told me how to combine them and prescribed times of rest. It gave me an oven @@ -93,13 +93,13 @@ apply in all cases and as React and things under it change our recipes would fall out-of-date. So like the bread, to produce consistently good results we need to understand how React does what it does. -** Basic React +** React, made of Conceptually React is very simple. It starts by walking a tree of components and building up a tree of their output. Then it compares that tree to the tree currently in the browser's DOM to find any differences between them. When it finds differences it updates the -browser's DOM to match its tree. +browser's DOM to match its internal tree. But what does that actually look like? If your app is janky does that explanation point you towards what is wrong? No. It might make you @@ -108,22 +108,31 @@ the diffing React does is slow but you won't really know. When I was initially testing out different bread recipes I had guesses at why it wasn't working but I didn't really figure it out until I had a deeper understanding of how making bread worked. It's time we build up our -understand of how React works so that we can start to answer our +understanding of how React works so that we can start to answer our questions with solid answers. -React is made up of a few pieces: ~createElement~, ~render~, and -reconciliation. The first building block is ~createElement~. While -~createElement~ is itself unlikely to be a bottleneck it's a good to -understand how it works so that we can have a complete picture of the -entire process. The more black-boxes we have in our mental model the -harder it will be for us to diagnose performance problems. - -** ~JSX~ - -But before we get to ~createElement~ we should talk about JSX. While -not strictly a part of React it is almost universally used with -it. And if we understand it then ~createElement~ will be less of a -mystery since we will be able to connect all the dots. +React is centered around the ~render~ method. The ~render~ method is +what walks our trees, diffs them with the browser's DOM tree, and +updates the DOM as needed. But before we can look at the ~render~ +method me have to understand its input. The input comes from +~createElement~. While ~createElement~ itself is unlikely to be a +bottleneck it's a good to understand how it works so that we can have +a complete picture of the entire process. The more black-boxes we have +in our mental model the harder it will be for us to diagnose +performance problems. + +** Markup in Javascript: ~JSX~ + +~createElement~, however, takes as input something that is probably +not familiar to us since we usually work in JSX, which is the last +element of the chain in this puzzle and the first step in solving +it. While not strictly a part of React it is almost universally used +with it. And if we understand it then ~createElement~ will be less of +a mystery since we will be able to connect all the dots. + +JSX is not valid HTML or Javascript but its own language compiled by a +compiler, like Babel. The output of that compilation is valid +Javascript that represents the original markup. Before JSX the normal way of injecting HTML into the DOM was via directly utilizing the browser's DOM APIs. This was very cumbersome. @@ -141,16 +150,6 @@ a DOM tree and can also be used to insert one into the browser's DOM. And to do that we need to understand what a tree of DOM nodes is constructed of. What parts do you see here? -TODO include text element (Hello) -TODO change to using objects instead of arrays? -probably do after what we've already done - -{ - type: 'h1', - props: { x: y }, - children: [] -} - #+BEGIN_SRC html