LearnContact
Lesson 1423 min read

Props

Learn what props are, how to pass and read them, destructuring, default values, the special children prop, and why props are read-only.

Introduction

So far your components have mostly displayed fixed, hardcoded content. But real components need to display different data depending on where they are used — a ProductCard should show a different product each time, and a Greeting should greet a different person. Props solve exactly this problem.

Props are how data flows into a component from the outside, making components truly reusable and configurable. This lesson covers everything you need to know about passing, reading, and safely working with props.

What You Will Learn
  • What props are and how they compare to function arguments.
  • How to pass props to a component and read them inside it.
  • How to destructure props directly in the function signature.
  • How to set default values for props.
  • What the special children prop is and how it works.
  • Why props must always be treated as read-only.

What Are Props?

Props (short for "properties") are data passed from a parent component to a child component. You can think of props as very similar to arguments passed into a regular JavaScript function — the parent decides what values to send in, and the child uses those values to decide what to render.

Flow Downward

Props always travel from parent to child — never in the opposite direction.

Bundled as an Object

All props passed to a component arrive together as a single object.

Configure Components

Props let the same component render differently depending on what is passed in.

Read-Only

A component must never modify the props object it receives.

Passing and Reading Props

You pass props to a component the same way you set attributes on an HTML element — as name="value" pairs in JSX. Inside the component, all the props arrive bundled together as a single object, usually named props.

Passing Props
function App() {
  return (
    <div>
      <Greeting name="Amol" role="Student" />
      <Greeting name="Priya" role="Instructor" />
    </div>
  );
}
Reading Props Inside the Component
function Greeting(props) {
  return (
    <h2>
      Hello, {props.name}! You are logged in as {props.role}.
    </h2>
  );
}
Rendered on the Page
Hello, Amol! You are logged in as Student.
Hello, Priya! You are logged in as Instructor.

Destructuring Props

Writing props.name and props.role repeatedly gets verbose. Most React code destructures the needed values directly in the function's parameter list, which is both shorter and clearer about exactly which props a component expects.

Destructuring in the Function Signature
function Greeting({ name, role }) {
  return (
    <h2>
      Hello, {name}! You are logged in as {role}.
    </h2>
  );
}
Why Destructure?

Destructuring documents exactly which props a component uses right in its signature, making the component easier to understand at a glance without reading the whole function body.

Default Prop Values

Sometimes a prop is optional, and you want the component to fall back to a sensible value if it is not provided. JavaScript's default parameter syntax works perfectly for this when destructuring props.

Default Values via Destructuring
function Greeting({ name, role = 'Guest' }) {
  return (
    <h2>
      Hello, {name}! You are logged in as {role}.
    </h2>
  );
}

// Usage — no role passed, so it defaults to "Guest"
<Greeting name="Sam" />
Rendered on the Page
Hello, Sam! You are logged in as Guest.

The default value is only used when the prop is undefined — if a parent explicitly passes role="" or role={null}, the default will not kick in for those values.

The Special children Prop

Every component automatically receives a special prop called children, containing whatever JSX was nested between its opening and closing tags. This is what lets you build wrapper components like cards, layouts, or modals that display whatever content is placed inside them.

Using the children Prop
function Card({ children }) {
  return <div className="card">{children}</div>;
}

function App() {
  return (
    <Card>
      <h3>Special Offer</h3>
      <p>Get 20% off this week only.</p>
    </Card>
  );
}
Rendered on the Page
Special Offer
Get 20% off this week only.
Think "Slot"

The children prop behaves like a content "slot" — the Card component does not need to know what is inside it, only that it should render whatever is passed between its tags.

Props Are Read-Only

React has a strict rule: a component must never modify its own props. Props represent data owned by the parent, and changing them inside the child would break React's ability to predictably track and update the UI. If a component needs to change data over time, it should use state instead — covered in the next lesson.

Correct: Treat Props as Read-Only

jsx
function Greeting({ name }) {
  const displayName = name.toUpperCase();
  return <h2>Hello, {displayName}!</h2>;
}

Incorrect: Mutating Props

jsx
function Greeting(props) {
  props.name = props.name.toUpperCase(); // never do this
  return <h2>Hello, {props.name}!</h2>;
}

Example: A Reusable Card Component

Putting several of these ideas together, here is a reusable ProfileCard component using destructuring, a default value, and the children prop.

ProfileCard.jsx
function ProfileCard({ name, title = 'Member', children }) {
  return (
    <div className="profile-card">
      <h3>{name}</h3>
      <p>{title}</p>
      {children}
    </div>
  );
}

function App() {
  return (
    <ProfileCard name="Amol Chipte" title="Course Author">
      <button>View Profile</button>
    </ProfileCard>
  );
}
Rendered on the Page
Amol Chipte
Course Author
[View Profile button]

Common Mistakes

Avoid These Mistakes
  • Modifying props directly inside a component instead of deriving a new value from them.
  • Forgetting that props flow only downward — a child cannot directly change data in its parent through props.
  • Assuming a default value applies to every "empty" input, when it really only applies to undefined.
  • Overloading a single component with too many unrelated props instead of splitting it into smaller components.

Best Practices

  • Destructure the props you need directly in the function signature for clarity.
  • Provide sensible default values for optional props.
  • Use the children prop for components that wrap or lay out other content.
  • Never mutate props — derive new local values instead if you need a transformed version.
  • Keep the number of props on a single component reasonable; group related values into an object if the list grows too long.

Frequently Asked Questions

Can props be any type of value, not just strings?

Yes. Props can be strings, numbers, booleans, arrays, objects, or even functions. Non-string values are passed using curly braces, like count={5} or onClick={handleClick}.

What happens if I do not pass a required prop?

The prop will simply be undefined inside the component, which can cause errors if the component tries to use it without a default value or a check.

Is children always JSX?

Usually, but it can also be plain text, a single element, an array of elements, or even a function in more advanced patterns. Most commonly, it is whatever JSX is nested between a component's tags.

Can a child component change a prop it received?

No — props are read-only. If the child needs to trigger a change, the parent typically passes down a function as a prop that the child can call, and the parent updates its own state in response.

Are props similar to function parameters?

Yes, that is a great mental model — a component is like a function, and props are like the arguments passed into it.

Key Takeaways

  • Props are data passed from a parent component down to a child, similar to function arguments.
  • Props arrive bundled as a single object and are commonly destructured in the function signature.
  • Default values can be set for optional props directly during destructuring.
  • The special children prop holds whatever JSX is nested between a component's opening and closing tags.
  • Props are strictly read-only — a component must never modify its own props.

Summary

Props are the mechanism that makes components configurable and reusable, letting a parent pass data down into a child much like arguments passed into a function. Destructuring, default values, and the children prop all make working with props cleaner and more expressive.

In this lesson, you learned what props are, how to pass and read them, how to destructure and default them, how the children prop works, and why props must always stay read-only. Next, you will learn about state — the data a component manages internally that can change over time.

Lesson 14 Completed
  • You can pass and read props confidently.
  • You can destructure props and provide default values.
  • You understand the special children prop.
  • You are ready to explore state in the next lesson.
Next Lesson →

State