Components

Loading concept...

๐Ÿงฑ React Components: Building Blocks of Your UI

The Big Idea: Think of React components like LEGO blocks. Each block is small, colorful, and does one thingโ€”but when you snap them together, you can build castles, spaceships, or anything you imagine!


๐ŸŽฏ What Youโ€™ll Learn

  • โœ… Function components (your building blocks)
  • โœ… Component composition (snapping blocks together)
  • โœ… Component hierarchy (big blocks contain small blocks)
  • โœ… UI as a tree (the family tree of your app)
  • โœ… Thinking in React (the secret recipe)

๐Ÿ  The LEGO Analogy

Imagine you want to build a toy house:

  • You donโ€™t carve it from one giant block of wood
  • You use many small LEGO pieces
  • Each piece has a purpose (doors, windows, walls)
  • You snap them together to make something amazing!

React works the same way!


1๏ธโƒฃ Function Components: Your Building Blocks

What Is a Function Component?

A function component is a tiny machine that:

  1. Takes some information (called โ€œpropsโ€)
  2. Spits out what to show on screen

Itโ€™s just a JavaScript function that returns HTML-like code!

Simple Example

function Greeting() {
  return <h1>Hello, Friend!</h1>;
}

Thatโ€™s it! This tiny function shows โ€œHello, Friend!โ€ on screen.

With Props (Information)

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

// Using it:
<Greeting name="Luna" />
// Shows: Hello, Luna!

Think of it like:

  • Props = the instructions you give your LEGO piece
  • โ€œMake this window BLUEโ€ or โ€œMake this door REDโ€

Real Life Example

function Button(props) {
  return (
    <button style={{color: props.color}}>
      {props.label}
    </button>
  );
}

// Using it:
<Button color="blue" label="Click Me" />

2๏ธโƒฃ Component Composition: Snapping Blocks Together

The Magic of Putting Things Inside Things

Remember how a LEGO house has:

  • A roof (made of roof pieces)
  • Walls (made of wall pieces)
  • A door (one special piece)

In React, you put components inside other components!

Example: Building a Card

function Avatar(props) {
  return <img src={props.image} />;
}

function Title(props) {
  return <h2>{props.text}</h2>;
}

function Card(props) {
  return (
    <div className="card">
      <Avatar image={props.photo} />
      <Title text={props.name} />
    </div>
  );
}

See what happened?

  • Card contains Avatar AND Title
  • Each piece does ONE thing well
  • Together they make something bigger!

Why This Is Amazing

Without Composition With Composition
One giant mess Small, clean pieces
Hard to fix Easy to fix one piece
Canโ€™t reuse Use anywhere!

3๏ธโƒฃ Component Hierarchy: The Family Tree

Parent and Child Components

Just like families have parents and children, components do too!

App (Great-grandparent)
  โ””โ”€โ”€ Page (Grandparent)
        โ””โ”€โ”€ Card (Parent)
              โ”œโ”€โ”€ Avatar (Child)
              โ””โ”€โ”€ Title (Child)

The Rules:

  • ๐Ÿ”ต Parents contain children
  • ๐Ÿ”ต Parents pass props down to children
  • ๐Ÿ”ต Children canโ€™t change their parents

Example: A Simple App

function App() {
  return (
    <Page>
      <Header />
      <Content />
      <Footer />
    </Page>
  );
}
graph TD A[App] --> B[Page] B --> C[Header] B --> D[Content] B --> E[Footer]

4๏ธโƒฃ UI as a Tree: The Big Picture

Your Whole App Is a Tree!

Every React app looks like an upside-down tree:

  • One root at the top (usually App)
  • Branches going down (pages, sections)
  • Leaves at the bottom (buttons, text, images)
graph TD A[๐ŸŒณ App] --> B[๐Ÿ“„ HomePage] A --> C[๐Ÿ“„ AboutPage] B --> D[๐ŸŽจ Header] B --> E[๐Ÿ“ MainContent] B --> F[๐Ÿ‘ฃ Footer] E --> G[๐Ÿƒ Card] E --> H[๐Ÿƒ Card] G --> I[๐Ÿ–ผ๏ธ Avatar] G --> J[๐Ÿ“Œ Title]

Why Trees?

Trees Are Great Becauseโ€ฆ
Easy to find things (start at top, go down)
Clear structure (no tangles!)
Updates flow DOWN like water

5๏ธโƒฃ Thinking in React: The Secret Recipe

The 5-Step Recipe

When you see ANY design, follow these steps:

Step 1: Break It Into Boxes ๐Ÿ“ฆ

Look at your design and draw boxes around each piece.

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚     Header      โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”    โ”‚
โ”‚  โ”‚   SearchBar     โ”‚    โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜    โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”      โ”‚
โ”‚  โ”‚ ๐Ÿƒโ”‚ โ”‚ ๐Ÿƒโ”‚ โ”‚ ๐Ÿƒโ”‚      โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”˜      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Step 2: Name Each Box

Give each box a clear name:

  • Header
  • SearchBar
  • ProductCard
  • ProductList

Step 3: Build the Tree

Arrange them parent โ†’ child:

App
  โ””โ”€โ”€ Header
  โ””โ”€โ”€ SearchBar
  โ””โ”€โ”€ ProductList
        โ””โ”€โ”€ ProductCard
        โ””โ”€โ”€ ProductCard
        โ””โ”€โ”€ ProductCard

Step 4: Build Tiny Pieces First

Start with the smallest pieces (leaves of the tree):

function ProductCard(props) {
  return (
    <div className="card">
      <img src={props.image} />
      <h3>{props.name}</h3>
      <p>${props.price}</p>
    </div>
  );
}

Step 5: Combine Into Bigger Pieces

Now snap them together:

function ProductList(props) {
  return (
    <div className="list">
      {props.products.map(item =>
        <ProductCard
          key={item.id}
          name={item.name}
          price={item.price}
          image={item.image}
        />
      )}
    </div>
  );
}

๐ŸŽฎ Real World Example: A Chat App

Letโ€™s โ€œThink in Reactโ€ for a chat app!

Step 1: See the Boxes

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ChatApp                โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚ MessageList        โ”‚ โ”‚
โ”‚  โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ โ”‚
โ”‚  โ”‚  โ”‚ Message       โ”‚ โ”‚ โ”‚
โ”‚  โ”‚  โ”‚ โ”Œโ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ” โ”‚ โ”‚ โ”‚
โ”‚  โ”‚  โ”‚ โ”‚Avatarโ”‚Textโ”‚ โ”‚ โ”‚ โ”‚
โ”‚  โ”‚  โ”‚ โ””โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ”‚
โ”‚  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚  โ”‚ MessageInput       โ”‚ โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Step 2: The Component Tree

graph TD A[ChatApp] --> B[MessageList] A --> C[MessageInput] B --> D[Message] B --> E[Message] D --> F[Avatar] D --> G[MessageText]

Step 3: Build It!

function Avatar(props) {
  return <img src={props.src} />;
}

function MessageText(props) {
  return <p>{props.text}</p>;
}

function Message(props) {
  return (
    <div className="message">
      <Avatar src={props.avatar} />
      <MessageText text={props.text} />
    </div>
  );
}

function MessageList(props) {
  return (
    <div className="messages">
      {props.messages.map(msg =>
        <Message
          key={msg.id}
          avatar={msg.avatar}
          text={msg.text}
        />
      )}
    </div>
  );
}

function ChatApp() {
  return (
    <div className="chat">
      <MessageList messages={allMessages} />
      <MessageInput />
    </div>
  );
}

๐Ÿง  Quick Summary

Concept One-Line Explanation
Function Component A function that returns what to show
Composition Putting components inside components
Hierarchy Parent-child relationships
UI as Tree Your whole app is an upside-down tree
Thinking in React Break design โ†’ Name boxes โ†’ Build tree โ†’ Code small โ†’ Combine

๐Ÿš€ You Did It!

You now understand the foundation of React:

  1. โœ… Components are LEGO blocks
  2. โœ… Snap them together (composition)
  3. โœ… Arrange in parent-child (hierarchy)
  4. โœ… See the whole tree (UI structure)
  5. โœ… Follow the 5-step recipe (Thinking in React)

Next time you see ANY website or app, youโ€™ll start seeing the component tree everywhere! ๐ŸŒณ


๐Ÿ’ก Remember: Every amazing React app started as a simple tree of tiny components. Start small, think in blocks, and youโ€™ll build incredible things!

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.