Building Your Learning Module...
Getting things ready for you!
Find videos you like?
Save to resource drawer for future reference!
Before we dive into React, let's understand what a tree is. Trees are a way to represent relationships between things. Think of your computer's file system - folders contain files and other folders. That's a tree! Each item has a parent (except the root), and items can have children.
When you build a React app, you create components that contain other components. This creates a component tree - a hierarchical structure where each component can have parent and children components.
Root: App (purple) • Parents: Header, MainContent, Footer (blue) • Children: Article, Sidebar (green)
Each colored box is a component. Notice how they nest inside each other!
function Header() {
return (
<header className="header">
<h1>🌟 My Awesome Blog</h1>
</header>
);
}
function Article() {
return (
<article className="article">
<h2>Understanding React Trees 🌳</h2>
<p>React organizes your UI as a tree structure...</p>
</article>
);
}
function Sidebar() {
return (
<aside className="sidebar">
<h3>Recent Posts</h3>
<ul>
<li>React Basics</li>
<li>Component Trees</li>
</ul>
</aside>
);
}
function MainContent() {
return (
<main className="main-content">
<Article />
<Sidebar />
</main>
);
}
function Footer() {
return (
<footer className="footer">
<p>© 2024 My Blog</p>
</footer>
);
}
function App() {
return (
<div className="app">
<Header />
<MainContent />
<Footer />
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);Loading preview...
The component tree shows your source code structure, but the render tree shows what React actually creates. It includes only components, not HTML elements like <div> or <h1>.
Includes HTML elements
Only React components
Beyond components, React apps have a module dependency tree - how your JavaScript files import each other. This matters for bundling, loading, and optimizing your app.
Tools like Webpack, Vite, and Parcel analyze your module dependency tree to:
Add components and watch the tree grow in real-time
function ComponentNode({ name, level = 0, color }) {
return (
<div
className="node"
style={{
marginLeft: level * 30 + 'px',
animationDelay: level * 0.1 + 's'
}}
>
<div className={`component ${color}`}>
{name}
</div>
{level > 0 && <div className="branch"></div>}
</div>
);
}
function App() {
const [showHeader, setShowHeader] = React.useState(true);
const [showMain, setShowMain] = React.useState(true);
const [showFooter, setShowFooter] = React.useState(true);
const [showChildren, setShowChildren] = React.useState(true);
return (
<div className="app">
<div className="controls">
<button onClick={() => setShowHeader(!showHeader)}>
{showHeader ? '✅' : '❌'} Header
</button>
<button onClick={() => setShowMain(!showMain)}>
{showMain ? '✅' : '❌'} Main
</button>
<button onClick={() => setShowFooter(!showFooter)}>
{showFooter ? '✅' : '❌'} Footer
</button>
<button onClick={() => setShowChildren(!showChildren)}>
{showChildren ? '✅' : '❌'} Children
</button>
</div>
<div className="tree">
<h3>🌳 Component Tree</h3>
<ComponentNode name="App" level={0} color="root" />
{showHeader && <ComponentNode name="Header" level={1} color="blue" />}
{showMain && (
<>
<ComponentNode name="MainContent" level={1} color="blue" />
{showChildren && (
<>
<ComponentNode name="Article" level={2} color="green" />
<ComponentNode name="Sidebar" level={2} color="green" />
</>
)}
</>
)}
{showFooter && <ComponentNode name="Footer" level={1} color="blue" />}
</div>
<div className="info">
<p>Toggle components to see the tree structure change!</p>
<p className="subtitle">
{[showHeader, showMain, showFooter].filter(Boolean).length +
(showMain && showChildren ? 2 : 0)} components in tree
</p>
</div>
</div>
);
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(<App />);Loading preview...
How your components nest in source code
Shows parent-child relationships in your JSX. Includes both React components and HTML elements.
What React sees during rendering
Only includes React components, not HTML elements. Used by React to determine what to update.
How your files import each other
Shows file imports. Used by build tools for bundling, tree-shaking, and code splitting.