Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 16

Deconstruct UI into a Component Tree

Recommended resources

Hello! Welcome to the final lesson in our module on React Fundamentals.

Introduction

In our last lesson, we explored one-way data flow, the principle that data in React flows downwards from parent to child. We also saw how to complete this loop by passing callback functions upwards from child to parent, enabling user interactions to update data at the source.

Today, we'll put all the pieces from this module together. We're moving from the "how" of component communication to the "what" and "where" of component architecture. Your learning outcome is to structure a simple application by breaking the UI into a component tree. This is a foundational design skill in React. You'll learn how to look at a UI mockup, decompose it into a logical hierarchy of components, and translate that hierarchy into a well-organized project structure. This process is the first step you'll take when building any React application.

Given your background in software engineering, you'll find this process analogous to breaking down a complex problem into smaller, manageable functions or classes, guided by the principle of separation of concerns.


Thinking in Components: The UI as a Tree

React encourages us to think of our User Interfaces not as single, monolithic blocks of HTML, but as a collection of independent, reusable pieces called components. The relationship between these components—how they are nested and organized—can be visualized as a tree.

Just as a web browser parses HTML into a Document Object Model (DOM) tree, React constructs a Render Tree from your components. This tree represents the structure of your UI at any given moment.

Understanding Your UI as a Tree

The official React documentation has an excellent page that explains this concept. It will help you understand how React 'sees' your application's structure.

Please read the section titled The Render Tree. Focus on how a piece of JSX with nested components maps to a parent-child tree structure. Note the key insight that the render tree is composed of React components, not raw HTML tags, making it platform-agnostic.

Visualizing this tree helps us reason about data flow, performance, and the overall architecture of our application.

Basic React Component Tree Diagram
This diagram illustrates how a typical web application can be broken down into a tree of components, starting from a single root `App` component.

A Methodical Approach: Decomposing a UI

So, how do you go from a UI design to a component tree? The React team provides a simple, five-step process. For this lesson, we will focus on the very first and most crucial step.

Thinking in React

The 'Thinking in React' guide is a seminal piece of documentation that every React developer should read. It outlines the thought process for building a React app. We'll focus on the first step.

Please read Step 1: Break the UI into a component hierarchy. Pay close attention to the single responsibility principle: 'a component should ideally only be concerned with one thing.' Also, notice how the proposed component hierarchy mirrors the nesting in the UI mockup.

As the article suggests, the process involves:

  1. Drawing boxes around every distinct piece of the UI.
  2. Giving each box a name. These names will become your components.
  3. Arranging them into a hierarchy. If component B is inside component A in your drawing, then it becomes a child of A in the tree.

Let's look at another example. A complex UI element like a card can itself be a small component tree.

React Component Tree for a Card UI
This visual breakdown shows decomposition at a granular level. The `<Card>` component acts as a parent to its sections, and the `<CardHeader>` is itself a parent to `<CardTitle>` and `<CardSubtitle>`. This demonstrates how component composition can be applied at any scale.
Test your understanding!

Look at the following simple UI mockup for a blog post.

  • A main container for the post.
  • Inside, there's a title.
  • Below the title, there's an author information section with the author's name and avatar image.
  • Below that, there's the main body content of the post.
  • Finally, at the bottom, there's a comments section.
  • The comments section has a heading ("Comments") and a list of individual comments.
  • Each individual comment shows the commenter's name and their message.

Based on the "Thinking in React" methodology, what would be a logical component tree for this UI? Try drawing it out or listing it in a hierarchical format.

Show answer

A logical component tree would look something like this. Note that there can be variations, but the key is to apply the separation of concerns.

  • BlogPost (The root container for this feature)
    • PostTitle (Could just be an <h1> inside BlogPost, but a component is fine too)
    • AuthorInfo
    • PostBody
    • CommentsSection
      • CommentList
        • CommentItem (This would be rendered multiple times inside CommentList)

This structure cleanly separates the different parts of the UI. The CommentsSection is a great example of a component that manages a piece of the UI that is complex enough to have its own sub-components.


From Tree to Code: A Practical Example

Let's see how this decomposition translates into a real project structure. We'll use a classic to-do app as our case study. A typical to-do app UI might have:

  1. A form to add new tasks.
  2. A list of all the tasks.
  3. A summary of completed tasks.

Following our methodology, we can identify a component hierarchy:

  • App (The root component that holds everything)
    • AddTodoForm
    • TodoList
      • TodoItem (Rendered for each to-do in the list)
    • TodoSummary

How does this conceptual tree map to files and folders? A standard convention in React is to create a src/components directory to house your reusable components.

React Beginner Course 2025 (Vite, Tailwind CSS, TypeScript)

Let's watch a practical demonstration of refactoring a monolithic component into a more organized, multi-component structure. This video from Coding in Flow perfectly illustrates the process and its benefits.

Watch the segment from 01:11:28 to 01:17:07. The instructor starts with all the to-do list rendering logic inside the main App component. Observe how they: Identify the list rendering logic as a distinct concern. Create a new TodoList.tsx component file. Move the relevant JSX and logic into this new component. Refactor the App component to use the new <TodoList /> component. Pass the necessary data (todos) and callbacks (onCompletedChange, onDelete) down as props. The instructor refers to this as 'prop drilling,' a key concept related to component trees.

This video demonstrates the core workflow:

  1. You start with a conceptual breakdown of the UI.
  2. You create separate component files (e.g., TodoList.tsx, TodoItem.tsx).
  3. You implement the parent-child relationships using JSX nesting (e.g., <App> renders <TodoList>, which renders multiple <TodoItem>s).
  4. You use the principles we've already learned—props and callbacks—to wire them together, ensuring data flows correctly through your newly defined structure.

The Complete Picture: A Dismissible Alert

Let's conclude this module with an exercise that ties everything together: components, props, nesting, callbacks, and even a small preview of state management. You'll build a small, self-contained application structure with two components that need to communicate.

React Tutorial for Beginners

In this final exercise from the 'Programming with Mosh' tutorial, you are challenged to build a dismissible alert. This requires creating an Alert component and a Button component and making them interact. Follow along and try to solve it yourself before watching the solution.

Watch the segment from 01:14:12 to 01:19:19. The task is to make a button that, when clicked, shows an alert, and the alert itself has a close button to hide it. Pay close attention to the resulting structure: The App component acts as the parent and 'conductor'. It holds the logic for whether the alert is visible or not (this is a sneak peek at 'state'!). It renders both the <Button> and the <Alert> components. It passes a callback function (setAlertVisibility(true)) to the <Button>'s onClick prop. It passes another callback function (setAlertVisibility(false)) to the <Alert>'s onClose prop. This is a perfect microcosm of a React application structure.

This exercise demonstrates a complete component tree in action. The App component is the root of this small tree, and it manages the interaction between its two children, Button and Alert, orchestrating the application's behavior.


Conclusion

You have now reached the end of the second module and have learned the essential skill of structuring a React application. Decomposing a UI into a component tree is the starting point for building clean, maintainable, and scalable applications.

Key Takeaways:

  • React applications are best understood as a tree of components.
  • The process of structuring an app starts with decomposing the UI into logical, self-contained components, each with a single responsibility.
  • This conceptual tree is then translated into a file and folder structure, typically with a dedicated src/components directory.
  • The component hierarchy you design must support React's one-way data flow: data is passed down the tree via props, and events are communicated up the tree via callback functions.

Next Steps

We've now built a solid foundation. We know how to create components, how to compose them, and how to structure them into a logical tree. However, our components have been mostly static. The next critical step is to make them truly dynamic and interactive.

In the next module, State and Interactivity with useState, we will dive deep into the concept of state. You will learn how to give your components memory, allowing them to remember information and change over time in response to user input. This is where your React applications will truly come to life.