React Props, Children, and Component Composition
Learn to pass data and callbacks through React props, use children for flexible layouts, and compose small components without building giant prop APIs.
Typed props give each component a clear contract for the data it displays and the callbacks it can invoke. Children let callers supply flexible inner content, while named props work better for fixed roles such as a footer. The examples show how composition keeps layouts adaptable without turning one component into a set of conflicting flags, and when to split responsibilities.
React Props, Children, and Component Composition
Introduction
Props are values a parent passes to a child: display data, callbacks, or nested content. Composition arranges small components together so one component does not own every layout and variation.
Pass the information a component needs, then let the parent decide how pieces fit together.
This article shows how typed props, children, and composition create reusable interfaces while keeping responsibilities clear.
Pass data and callbacks with props
Props carry values down the tree. A callback gives a child a way to report an action while the parent keeps control of what happens next.
interface ProfileSummaryProps {
name: string;
role: string;
onOpenProfile: () => void;
}
function ProfileSummary({ name, role, onOpenProfile }: ProfileSummaryProps) {
return (
<section>
<h2>{name}</h2>
<p>{role}</p>
<button type="button" onClick={onOpenProfile}>
View profile
</button>
</section>
);
}
The parent can supply the profile values and decide whether the callback opens a route, a dialog, or another view.
Use children for nested content
React’s children prop carries content between a component’s opening and closing tags. It suits wrappers with a stable layout and changing inner content:
import type { ReactNode } from "react";
interface PanelProps {
title: string;
children: ReactNode;
}
function Panel({ title, children }: PanelProps) {
return (
<section className="panel">
<h2>{title}</h2>
<div>{children}</div>
</section>
);
}
Callers can put different content in the same panel:
<Panel title="Account">
<p>Manage your profile details.</p>
<SaveButton label="Save" onSave={saveProfile} />
</Panel>
Use children when callers choose the contents of a frame. Use named props for specific roles such as footer or icon.
When to Use / When NOT to Use
Use data props when a child displays a known value, and callbacks when it reports an action to its parent. Use children for wrappers whose inner content varies by caller. Avoid children for fixed named roles such as an icon or footer; a named prop gives that slot a clearer contract.
Compose small pieces
The difference is easier to see with a component that starts collecting flags.
Before: one component owns layout, display, and save behavior. The flags can also conflict.
interface ProfileCardProps {
name: string;
role: string;
compact: boolean;
showSave: boolean;
onSave: () => void;
}
function ProfileCard({
name,
role,
compact,
showSave,
onSave,
}: ProfileCardProps) {
return (
<section className={compact ? "compact" : "roomy"}>
<h2>{name}</h2>
<p>{role}</p>
{showSave && (
<button type="button" onClick={onSave}>
Save
</button>
)}
</section>
);
}
After: the wrapper owns its frame, the caller supplies its contents, and a button reports the action through a typed callback.
import type { ReactNode } from "react";
interface ProfilePanelProps {
name: string;
role: string;
children: ReactNode;
}
function ProfilePanel({ name, role, children }: ProfilePanelProps) {
return (
<section>
<h2>{name}</h2>
<p>{role}</p>
{children}
</section>
);
}
interface SaveButtonProps {
onSave: () => void;
}
function SaveButton({ onSave }: SaveButtonProps) {
return (
<button type="button" onClick={onSave}>
Save profile
</button>
);
}
function ProfilePage() {
const saveProfile = () => {
// The page owns the save operation.
};
return (
<ProfilePanel name="Mira Chen" role="Designer">
<p>Profile details can vary by caller.</p>
<SaveButton onSave={saveProfile} />
</ProfilePanel>
);
}
Here the page defines saveProfile and passes it down; SaveButton only reports the click. children fits because callers choose the panel’s inner content. If a component always has a footer or icon in a fixed position, give that role a named prop instead.
flowchart TD
A[Profile page] --> B[Panel layout]
B --> C[Profile details]
B --> D[Save button]
D --> E[Parent callback]
Composition keeps the layout visible without tying it to how profiles are saved.
Trade-Off Table
| Approach | Use it when | Trade-off |
|---|---|---|
| Many configuration props | Variations are few and closely related | Call sites grow harder to scan as flags multiply |
children |
The caller supplies flexible inner content | The wrapper cannot control the meaning of every child |
Named slots such as footer |
A few positions have clear roles | Adds props when each slot has a distinct purpose |
| Compose separate components | Pieces have separate jobs or ownership | Call sites can contain more markup |
Avoid a giant prop API of unrelated switches like showFooter, compact, and loading. Split distinct jobs into smaller components or let callers compose the content.
Production Failure Scenarios
Suppose a Card has flags for warnings, empty states, and loading. Conflicting flags show a warning beside a spinner. Keep props for coherent behavior; move distinct content into children or focused components. Model mutually exclusive states explicitly. A second failure occurs when a wrapper clones arbitrary children to inject a prop: text nodes or components that do not accept it can break. Prefer an explicit named slot or callback contract.
Security and Compliance Notes
Props are not a security boundary. Validate user supplied values at entry and avoid passing sensitive data to components that do not need it. React escapes text children; sanitize untrusted HTML before using dangerouslySetInnerHTML. Ordinary composition has no special compliance requirement, but keep private values out of client-visible props.
Common Pitfalls / Anti-Patterns
Do not mutate props or use a boolean where a named variant is clearer. Keep callback types specific and state with the component that owns the decision.
Observability Checklist
- Record a failed user action and request ID when network work fails.
- Avoid logging secrets or full user records.
- Skip telemetry for composition with no user-visible outcome.
Quick Recap Checklist
- Pass display values as data props and interactions as callback props.
- Use
childrenwhen callers supply flexible inner content. - Compose focused components when a component starts collecting unrelated flags.
- Keep state and side effects with the component that owns them.
- Validate untrusted input and avoid rendering untrusted HTML.
Interview Questions
A data prop gives a child a value to use. A callback tells the parent an interaction occurred, so the parent can decide what work to perform.
children?Use children when callers choose a wrapper's inner content. Use named props for specific roles such as a footer.
Callers combine focused components instead of toggling unrelated booleans, keeping each component's responsibility small.
It lets the child report an action while the parent keeps ownership of the resulting work.
- The child can call
onSave()when clicked. - The parent decides whether to save locally or make a request.
React.ReactNode covers values React can render as children.
- It supports text, elements, and collections of renderable values.
- Choose a narrower contract only when the component needs one specific shape.
Use a named prop when a piece of content has a fixed role in the wrapper.
- A
footerprop identifies that area directly. - Children work better for flexible inner content.
Children can include text or components that do not accept the injected prop.
- Use an explicit slot or typed callback contract.
- Keep parent and child responsibilities visible.
A focused variant or state type can prevent conflicting flags.
- Model one of loading, empty, or warning rather than several unrelated booleans.
- Compose separate components when the content has different jobs.
A specific signature tells the caller which information the child reports.
- A string ID is often enough for a selected record.
- A broad event or state object can couple the child to its parent.
No. Client props can be inspected and do not authorize actions.
- Validate untrusted input where it enters the application.
- Enforce permissions on the server for sensitive operations.
Further Reading
- Continue with typed function components to see prop contracts in component examples.
- See component boundaries and project structure for ways to split responsibilities as features grow.
- For interaction contracts, read React event handling and event types.
- Stable identity matters when composing repeated children; see React lists and stable keys.
- See sharing state and lifting state up for deciding which component owns shared state.
- Review accessible React interfaces for semantic controls and labels.
- React’s guides cover passing props, writing components, and using TypeScript with React.
Conclusion
Give components the data they need and let parents own their actions. Use children for flexible layouts, and split off flags when they start controlling unrelated behavior.
Category
Related Posts
Component Boundaries and React Project Structure
Organize React and TypeScript by feature, separate reusable UI from domain logic, and spot when a component has grown beyond one job.
Controlled Inputs and Forms in React
Learn to control form values with React state, type change and submit events, show validation feedback, and handle form data safely with TypeScript.
React Event Handling and Event Types
Learn to type React click, change, and form handlers in TypeScript, pass callbacks through props, and validate user input safely with clear examples.