Lesson 2 of 11 · 11 min · Components

Props, children and composition

Covers: React 19 with TypeScript. Updated 13 Nov 2026. Part of the Frontend Developer roadmap.

What you will learn

  • Pass data to components with props and read them with destructuring
  • Type props with TypeScript, including optional props and defaults
  • Use the special children prop for wrappers and layouts
  • Pass functions, elements and components as props
  • Build interfaces by composition rather than inheritance

In simple words

Props are the inputs of a component, the way function arguments are the inputs of a function. A parent decides what to pass; the child just displays it. The children prop is the content you put between a component's opening and closing tags, like putting a photo inside a frame: the frame (a Card or Modal) does not need to know what is inside. Building screens by plugging components into each other like this is called composition.

Words to know

  • Props: short for properties; the read-only inputs passed from a parent to a child component.
  • children: the content nested between a component's tags.
  • Composition: building complex components by combining simpler ones.
  • Controlled by the parent: the parent owns the data and decides what the child shows.

Passing and reading props

type CourseCardProps = {
  title: string;
  lessons: number;
  level?: "Beginner" | "Intermediate";    // optional
  free?: boolean;
};

export function CourseCard({ title, lessons, level = "Beginner", free = false }: CourseCardProps) {
  return (
    <article className="card">
      <h3>{title}</h3>
      <p>{lessons} lessons · {level}</p>
      {free && <span className="badge">Free</span>}
    </article>
  );
}

// usage
<CourseCard title="SQL for analysts" lessons={14} free />
<CourseCard title="Spring Boot" lessons={12} level="Intermediate" />
  • Attributes become properties of one props object; destructure it in the parameter list.
  • Strings can be written title="SQL"; everything else goes in braces: lessons={14}, free={true}, items={list}. A bare free means free={true}.
  • Default values use destructuring defaults (level = "Beginner").
  • Spread an object of props with <CourseCard {...course} /> (use sparingly: it hides what is passed).
  • In TypeScript, a type or interface for props documents the component and catches mistakes: a missing title or a wrong level fails to compile. Name it ComponentNameProps.

Props are read-only

A component must never modify its props. Props are the parent's data; changing them breaks the one-way flow. If a component needs data that changes, it uses state (next lesson) or asks the parent to change it by calling a function the parent passed in.

function Bad({ items }: { items: string[] }) {
  items.push("x");              // mutating a prop: don't
  return null;
}

One-way data flow

Data flows down the tree: parent to child through props. Events flow up through callback props: the child calls a function the parent gave it.

type LikeButtonProps = { liked: boolean; onToggle: () => void };

function LikeButton({ liked, onToggle }: LikeButtonProps) {
  return (
    <button type="button" aria-pressed={liked} onClick={onToggle}>
      {liked ? "Liked" : "Like"}
    </button>
  );
}

function Course() {
  const [liked, setLiked] = useState(false);               // the parent owns the state
  return <LikeButton liked={liked} onToggle={() => setLiked(!liked)} />;
}

Name callback props onSomething (onToggle, onSelect, onChange) and handler functions handleSomething. The child stays reusable and testable because it knows nothing about where the data lives.

The children prop

Whatever you put between a component's tags arrives as props.children:

import type { ReactNode } from "react";

type CardProps = { title: string; children: ReactNode };

function Card({ title, children }: CardProps) {
  return (
    <section className="card">
      <h2>{title}</h2>
      <div className="card-body">{children}</div>
    </section>
  );
}

<Card title="Your plan">
  <p>Starter pack: 4 interviews.</p>
  <button type="button">Upgrade</button>
</Card>

ReactNode is the type of anything React can render (elements, strings, numbers, arrays, null). Use children for wrappers and layouts: cards, modals, page shells, buttons with icons, error boundaries, context providers. The wrapper controls the structure and the caller controls the content.

Passing elements and components

Props can be any value, including JSX elements and functions, which gives you "slots":

type PageProps = { header: ReactNode; sidebar?: ReactNode; children: ReactNode };

function Page({ header, sidebar, children }: PageProps) {
  return (
    <div className="page">
      <header>{header}</header>
      {sidebar && <aside>{sidebar}</aside>}
      <main>{children}</main>
    </div>
  );
}

<Page header={<NavBar />} sidebar={<Filters />}>
  <CourseList />
</Page>

Render props pass a function that returns JSX, letting the child decide when to call it with its own data:

type ListProps<T> = { items: T[]; render: (item: T) => ReactNode };

function List<T>({ items, render }: ListProps<T>) {
  return <ul>{items.map((item, i) => <li key={i}>{render(item)}</li>)}</ul>;
}

<List items={courses} render={(c) => <strong>{c.title}</strong>} />

You can also pass a component itself as a prop (icon: ComponentType<{ size: number }>) and render it as <Icon size={16} />.

Composition instead of inheritance

React does not use class inheritance to share UI. The advice from the React team: use composition. Build a general component and specialise it by wrapping:

function Button({ children, variant = "primary", ...rest }: ButtonProps) {
  return <button className={`btn btn-${variant}`} {...rest}>{children}</button>;
}

function SubmitButton() {
  return <Button type="submit" variant="primary">Save</Button>;     // a specialised use
}

function ConfirmDialog({ onConfirm, onCancel }: DialogProps) {
  return (
    <Dialog title="Delete this resume?">
      <p>This cannot be undone.</p>
      <Button variant="secondary" onClick={onCancel}>Cancel</Button>
      <Button variant="danger" onClick={onConfirm}>Delete</Button>
    </Dialog>
  );
}

Helpful patterns:

  • Container and presentational: one component fetches and owns data, another purely displays it (now often replaced by hooks).
  • Compound components: related parts share state through context (<Tabs><Tabs.List>...</Tabs.List><Tabs.Panel>...</Tabs.Panel></Tabs>).
  • Custom hooks (later lessons) share logic, composition handles structure.
  • Pass through attributes to the underlying element with the rest operator and the right TypeScript type (React.ComponentProps<"button">), so your Button accepts disabled, aria-label, onClick and so on:
type ButtonProps = React.ComponentProps<"button"> & { variant?: "primary" | "secondary" | "danger" };

React 19 treats ref as an ordinary prop for function components, so forwardRef is no longer needed.

Prop design guidelines

  • Keep the number of props small; many props often mean the component does too much, or that it should accept children or a slot.
  • Prefer a few meaningful, typed props over a generic config object.
  • Use union types for variants (size: "sm" | "md" | "lg") rather than several booleans (isSmall, isLarge) that can contradict each other.
  • Pass only what the child needs (the user's name, not the whole user object), unless the child is truly about the whole object.
  • Avoid prop drilling through many layers (passing a value through five components that do not use it): use composition (pass the finished element down), or context (the state lesson).
  • Do not copy a prop into state without a reason (useState(props.value) ignores later changes to the prop).
  • Make accessibility part of the API (require alt, label or aria-label props where needed).

Common mistakes

  • Mutating props.
  • Passing a function call instead of a function (onClick={handle()} runs it during render; use onClick={handle} or onClick={() => handle(id)}).
  • Prop drilling through many layers instead of composition or context.
  • Forgetting braces for numbers and booleans (lessons="14" is the string "14").
  • Many boolean props that can combine into invalid states.
  • Copying props into state and getting stale values.
  • Spreading all props onto DOM elements, including unknown ones.

Try it yourself

  1. Make a CourseCard with typed props (title, lessons, optional level with a default, optional free).
  2. Build a Card wrapper with children and use it for three different contents.
  3. Make a LikeButton that receives liked and onToggle, and keep the state in the parent.
  4. Build a Page component with header and sidebar slots.
  5. Make a Button that forwards native button attributes and has variant as a union type.

Check yourself

Why are props read-only?

Data flows one way, from parent to child; if a child changed its props, it would break that flow and make the parent's data unpredictable. Changes go through state or callbacks.

What is the children prop?

The content between a component's opening and closing tags, made available to the component so it can wrap or place it.

What is the difference between onClick={handle} and onClick={handle()}?

handle passes the function to be called on click; handle() calls it immediately during render and passes its result.

What is prop drilling and how can you avoid it?

Passing a prop through many intermediate components that do not use it; avoid with composition (passing finished elements as children or slots) or with context.

Interview questions

How do components communicate in React?

Parents pass data to children through props, and children notify parents through callback props; siblings share data by lifting state to a common parent, or through context or a state library.

Explain composition versus inheritance in React.

React builds UIs by combining components (children, slots, render props, wrappers) rather than extending classes; it is more flexible and avoids rigid hierarchies.

When would you use the children prop instead of other props?

When the component is a generic wrapper or layout (card, modal, page shell) that should not know its content, so callers supply anything they like inside it.

Summary

  • Props are read-only inputs; destructure and type them; use defaults and union types for variants.
  • Data flows down through props, events flow up through callback props.
  • children, slots and render props enable flexible composition; prefer them to inheritance.
  • Avoid prop drilling, mutated props and copying props into state.

Going deeper

See how much of this you can explain

A free mock interview asks you new questions on React: components, state, routing and testing for Frontend Developer and scores every answer, with what to fix.