Skip to main content

Command Palette

Search for a command to run...

Components and Props in React

Published
β€’3 min readβ€’View as Markdown
Components and Props in React
A

I’m Arjun Saxena, a passionate software developer specializing in web engineering. I believe in writing code that creates real solutions to real problems. I love building efficient, user-friendly applications and constantly push myself to learn new technologies. Beyond coding, I enjoy sharing knowledge and growing together with others in the tech community.

Building UI Like Lego Blocks (Beginner Friendly Guide)

React is all about components and props.
Once you understand these two, React becomes easy and fun.

In this blog, we’ll learn:

  • What React components are

  • How props work (parent β†’ child communication)

  • Children props and default props
    All explained in a very easy, real-life way.

What Is a React Component? πŸ€”

A React component is a small reusable piece of UI.

Simple Definition

A component is a JavaScript function that returns HTML-like code (JSX).

Components = Lego Blocks 🧱 (Best Analogy)

Think of a website like a Lego structure:

Website PartReact Concept
Lego blockComponent
Reusing same blockReusable component
Combining blocksComponent composition

πŸ“Œ Once you build one block, you can reuse it anywhere.

Your First React Component

function Welcome() {
  return <h1>Hello, Welcome to React!</h1>;
}

βœ” This is a component
βœ” It returns JSX
βœ” It can be reused multiple times

Using the Component

<Welcome />

That’s it πŸŽ‰

Why Components Are Important?

  • Reusable

  • Easy to manage

  • Clean code

  • Easy to debug

  • Scales well for large apps

Parent and Child Components (Hierarchy)

Most React apps look like a tree:

https://miro.medium.com/v2/resize%3Afit%3A1400/1%2AonyDxu9rALAl4DF1ZXhKRw.png

https://miro.medium.com/v2/resize%3Afit%3A1270/1%2Amll5wY6YI5BXJ-M3ne6qpA.png

https://cevo.com.au/wp-content/uploads/2024/10/Picture8.png

Example:

<App>
  β”œβ”€β”€ Header
  β”œβ”€β”€ ProductList
  β”‚     └── ProductCard
  └── Footer

Understanding Props in React 🎁

Props (short for properties) are used to pass data from parent to child component.

Simple Definition

Props are like arguments passed to a function.

Real-Life Example: Gift 🎁

  • Parent β†’ gives gift

  • Child β†’ receives gift

  • Child cannot change the gift

πŸ“Œ Props are read-only.

Passing Props (Simple Example)

Parent Component

function App() {
  return <Greeting name="Arjun" />;
}

Child Component

function Greeting(props) {
  return <h2>Hello {props.name} πŸ‘‹</h2>;
}

βœ” Parent sends data
βœ” Child receives via props

Output

Hello Arjun πŸ‘‹

Props with Destructuring (Cleaner Way)

function Greeting({ name }) {
  return <h2>Hello {name} πŸ‘‹</h2>;
}

βœ” Cleaner
βœ” More readable

Props Flow Direction (Important Rule)

❗ Props flow one-way only

Parent β†’ Child βœ…
Child β†’ Parent ❌

This makes React apps predictable and stable.

Using Multiple Props

function Profile({ name, age }) {
  return (
    <p>
      Name: {name}, Age: {age}
    </p>
  );
}
<Profile name="Arjun" age={22} />

Children Props πŸ§’

Passing JSX Inside Components

Sometimes you want to pass content between component tags.

Example: Card Component

function Card({ children }) {
  return (
    <div style={{ border: "1px solid gray", padding: "10px" }}>
      {children}
    </div>
  );
}

Using Card Component

<Card>
  <h2>Hello!</h2>
  <p>This is inside the card.</p>
</Card>

πŸ“Œ Whatever you write inside <Card> becomes children.

Default Props πŸ›‘οΈ

When Props Are Not Passed

Default props provide fallback values.

Example

function Button({ text = "Click Me" }) {
  return <button>{text}</button>;
}
<Button />
<Button text="Submit" />

βœ” First button β†’ β€œClick Me”
βœ” Second button β†’ β€œSubmit”

Component Reusability Example πŸ”

https://cdn.buttercms.com/nrv8waPWSgyYbiaDEoKS

https://screenshots.codesandbox.io/dczs4/13.png

https://miro.medium.com/1%2AmKo0x3xJKCaFdS_sv_2Akw.png

function Button({ text }) {
  return <button>{text}</button>;
}
<Button text="Login" />
<Button text="Signup" />
<Button text="Logout" />

βœ” One component
βœ” Multiple uses

Common Beginner Mistakes 🚫

❌ Modifying props
❌ Forgetting to pass props
❌ Using props instead of state
❌ Overcomplicating components