Chapter 4: Props

1. What are Props? (The most important concept)

Props = Properties They are read-only data that a parent component passes down to a child component.

Analogy: Imagine a toy factory that makes customizable teddy bears. The factory (parent component) decides:

  • What color fur?
  • What size eyes?
  • What message on the t-shirt?

These choices are props. The teddy bear machine (child component) just receives them and builds the bear accordingly — it can’t change the instructions.

Important rules about props:

  • Props are read-only inside the child component (you cannot do props.name = “new name”)
  • Props flow one way: parent → child (never child → parent directly)
  • Props can be any JavaScript value: string, number, boolean, object, array, function, even other components!

2. Passing Data with Props (Basic Example)

Let’s create a reusable UserCard component.

Create new file: src/components/UserCard.tsx

tsx

Now use it in App.tsx (multiple times with different data!):

tsx

Boom! Same component → 3 completely different cards. That’s the power of props.

3. Props Destructuring (Cleaner & Modern Way)

Most React developers never use props.xxx — they destructure props.

Improved version of UserCard.tsx:

tsx

Even shorter (with default values directly in destructuring — we’ll talk about defaults soon):

tsx

4. Default Props & Prop Types (Type Safety + Defaults)

A. Default Props (What happens if parent forgets to pass a prop?)

Two modern ways in 2026:

Way 1: Default values in destructuring (most popular)

tsx

Way 2: Using defaultProps (older way, still works)

tsx

B. Prop Types (Optional but very helpful for catching bugs)

Install prop-types (if you want classic runtime checking):

Bash
tsx

But honestly in 2026 with TypeScript → you don’t need prop-types anymore because TypeScript already gives you compile-time safety + editor autocompletion.

5. Children Props (Super Powerful!)

Sometimes you want to pass JSX or content between opening and closing tags.

Example: Create a FancyBox component

src/components/FancyBox.tsx

tsx

Use it like this in App.tsx:

tsx

Your Mini Exercise (Do this now!)

  1. Create a new component ProfileCard.tsx
  2. It should accept props:
    • name (string, required)
    • role (string, default: “React Learner”)
    • bio (string)
    • skills (array of strings)
  3. Use destructuring
  4. Show a nice card with name, role, bio, and a list of skills
  5. Use it twice in App.tsx with different data
  6. Bonus: Wrap one of them inside a FancyBox

Summary – Chapter 4 Key Takeaways

  • Props = data passed from parent to child
  • Always destructure props — cleaner code
  • Use TypeScript interfaces for prop types (best in 2026)
  • Default values in destructuring = easiest way
  • children prop lets you pass content between tags
  • Props are read-only — never mutate them inside child

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *