You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/learn/describing-the-ui.md
+24Lines changed: 24 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -18,6 +18,7 @@ React is a JavaScript library for rendering user interfaces (UI). UI is built fr
18
18
*[How to conditionally render components](/learn/conditional-rendering)
19
19
*[How to render multiple components at a time](/learn/rendering-lists)
20
20
*[How to avoid confusing bugs by keeping components pure](/learn/keeping-components-pure)
21
+
*[Why understanding your UI as trees is useful](/learn/understanding-your-ui-as-a-tree)
21
22
22
23
</YouWillLearn>
23
24
@@ -523,6 +524,29 @@ Read **[Keeping Components Pure](/learn/keeping-components-pure)** to learn how
523
524
524
525
</LearnMore>
525
526
527
+
## Your UI as a tree {/*your-ui-as-a-tree*/}
528
+
529
+
React uses trees to model the relationships between components and modules.
530
+
531
+
A React render tree is a representation of the parent and child relationship between components.
532
+
533
+
<Diagramname="generic_render_tree"height={250}width={500}alt="A tree graph with five nodes, with each node representing a component. The root node is located at the top the tree graph and is labelled 'Root Component'. It has two arrows extending down to two nodes labelled 'Component A' and 'Component C'. Each of the arrows is labelled with 'renders'. 'Component A' has a single 'renders' arrow to a node labelled 'Component B'. 'Component C' has a single 'renders' arrow to a node labelled 'Component D'.">An example React render tree.</Diagram>
534
+
535
+
Components near the top of the tree, near the root component, are considered top-level components. Components with no child components are leaf components. This categorization of components is useful for understanding data flow and rendering performance.
536
+
537
+
Modelling the relationship between JavaScript modules is another useful way to understand your app. We refer to it as a module dependency tree.
538
+
539
+
<Diagramname="generic_dependency_tree"height={250}width={500}alt="A tree graph with five nodes. Each node represents a JavaScript module. The top-most node is labelled 'RootModule.js'. It has three arrows extending to the nodes: 'ModuleA.js', 'ModuleB.js', and 'ModuleC.js'. Each arrow is labelled as 'imports'. 'ModuleC.js' node has a single 'imports' arrow that points to a node labelled 'ModuleD.js'.">An example module dependency tree.</Diagram>
540
+
541
+
A dependency tree is often used by build tools to bundle all the relevant JavaScript code for the client to download and render. A large bundle size regresses user experience for React apps. Understanding the module dependency tree is helpful to debug such issues.
Read **[Your UI as a Tree](/learn/understanding-your-ui-as-a-tree)** to learn how to create a render and module dependency trees for a React app and how they're useful mental models for improving user experience and performance.
546
+
547
+
</LearnMore>
548
+
549
+
526
550
## What's next? {/*whats-next*/}
527
551
528
552
Head over to [Your First Component](/learn/your-first-component) to start reading this chapter page by page!
Copy file name to clipboardExpand all lines: src/content/learn/understanding-your-ui-as-a-tree.md
+7-5Lines changed: 7 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -136,7 +136,7 @@ The root node in a React render tree is the [root component](/learn/importing-an
136
136
137
137
You'll notice in the above render tree, there is no mention of the HTML tags that each component renders. This is because the render tree is only composed of React [components](learn/your-first-component#components-ui-building-blocks).
138
138
139
-
React, as a UI framework, is platform agnostic. On react.dev, we showcase examples that render to the web, which uses HTML markup as its UI primitives. But a React app could just as likely render to a mobile or desktop platform, which may use different UI primitives like [UIView](https://developer.apple.com/documentation/uikit/uiview) or [Framework Element](https://learn.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement?view=windowsdesktop-7.0).
139
+
React, as a UI framework, is platform agonistic. On react.dev, we showcase examples that render to the web, which uses HTML markup as its UI primitives. But a React app could just as likely render to a mobile or desktop platform, which may use different UI primitives like [UIView](https://developer.apple.com/documentation/uikit/uiview) or [FrameworkElement](https://learn.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement?view=windowsdesktop-7.0).
140
140
141
141
These platform UI primitives are not a part of React. React render trees can provide insight to our React app regardless of what platform your app renders to.
142
142
@@ -252,11 +252,13 @@ With conditional rendering, across different renders, the render tree may render
252
252
253
253
In this example, depending on what `inspiration.type` is, we may render `<FancyText>` or `<Color>`. The render tree may be different for each render pass.
254
254
255
-
Although render trees may differ across render pases, these trees are generally helpful for identifying what the top-level and leaf components are in a React app. Top-level components are the components nearest to the root component and affect the rendering performance of all the components beneath them and often contain the most complexity. Leaf components are near the bottom of the tree and have no child components and are often frequently re-rendered. Identifying these categories of components are useful for understanding data flow and performance of your app.
255
+
Although render trees may differ across render pases, these trees are generally helpful for identifying what the top-level and leaf components are in a React app. Top-level components are the components nearest to the root component and affect the rendering performance of all the components beneath them and often contain the most complexity. Leaf components are near the bottom of the tree and have no child components and are often frequently re-rendered.
256
+
257
+
Identifying these categories of components are useful for understanding data flow and performance of your app.
256
258
257
259
## The Module Dependency Tree {/*the-module-dependency-tree*/}
258
260
259
-
Another relationship in a React app that can be modeled with a tree are an app's module dependencies. As we [break up our components](/learn/importing-and-exporting-components#exporting-and-importing-a-component) and logic into separate files, we create [JS modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) where we may export components, functions or constants.
261
+
Another relationship in a React app that can be modeled with a tree are an app's module dependencies. As we [break up our components](/learn/importing-and-exporting-components#exporting-and-importing-a-component) and logic into separate files, we create [JS modules](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules) where we may export components, functions, or constants.
260
262
261
263
Each node in a module dependency tree is a module and each branch represents an `import` statement in that module.
262
264
@@ -272,9 +274,9 @@ The root node of the tree is the root module, also known as the entrypoint file.
272
274
273
275
Comparing to the render tree of the same app, there are similar structures but some notable differences:
274
276
275
-
* The nodes that make-up the tree represent modules, versus components.
277
+
* The nodes that make-up the tree represent modules, not components.
276
278
* Non-component modules, like `inspirations.js`, are also represented in this tree. The render tree only encapsulates components.
277
-
*`Copyright.js` appears under `App.js` but in the render tree, `Copyright`, the component, appears as a child of `InspirationGenerator`. This is because `InspirationGenerator` accepts JSX as children props.
279
+
*`Copyright.js` appears under `App.js` but in the render tree, `Copyright`, the component, appears as a child of `InspirationGenerator`. This is because `InspirationGenerator` accepts JSX as [children props](/learn/passing-props-to-a-component#passing-jsx-as-children), so it renders `Copyright` as a child component but does not import the module.
278
280
279
281
Dependency trees are useful to determine what modules are necessary to run your React app. When building a React app for production, there is typically a build step that will bundle all the necessary JavaScript to ship to the client. The tool responsible for this is called a [bundler](https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Understanding_client-side_tools/Overview#the_modern_tooling_ecosystem), and bundlers will use the dependency tree to determine what modules should be included.
0 commit comments