React Props and Children
Pass data down the component hierarchy using immutable props, destructuring patterns, and special children composition.
TL;DR
- Pass data downward into child components using immutable
propsarguments. - Destructure incoming properties with sensible fallback
defaultsin signatures. - Nest visual layout content between element tags using
children.
Passing and Reading Props
Direct Props ObjectReceive the full props object as the first function argument.
export function Badge(props: { text: string }) {
return <BadgeView text={props.text} />;
}Destructured PropsUnpack specific keys directly inside parameter parenthesis.
export function Badge({ text }: { text: string }) {
return <BadgeView text={text} />;
}Default Prop ValuesProvide sensible fallback values for optional props.
function Alert({ role = 'info' }: AlertProps) {
return <AlertBox className={`alert-${role}`} />;
}Children Prop Composition
Container WrapperRender nested child JSX nodes using the children prop.
export function Box({
children,
}: { children: React.ReactNode }) {
return <BoxFrame>{children}</BoxFrame>;
}Call Site NestingNest custom elements or text between component tags.
<Box>
<CardHeader>Card Heading</CardHeader>
<CardBody>Nested child content.</CardBody>
</Box>Multiple Slot PropsPass React nodes via named props for distinct visual slots.
export function Panel({
header,
footer,
}: PanelProps) {
return <CardView top={header} bot={footer} />;
}Advanced Prop Patterns
Rest Prop SpreadingForward remaining HTML attributes to underlying DOM nodes.
function Input({ label, ...rest }: InputProps) {
return <InputBase {...rest} />;
}Callback PropsPass handler functions to allow children to report events.
function DeleteBtn({ onConfirm }: ActionProps) {
return <Button onClick={onConfirm}>Delete</Button>;
}Boolean Prop FlagPassing a prop name without value evaluates to true.
<Button primary outlined />TypeScript Prop Contracts
Interface ContractDeclare explicit prop shapes with optional properties.
interface ButtonProps {
title: string;
disabled?: boolean;
onClick: () => void;
}PropsWithChildren HelperStandard generic type including the children property.
import { PropsWithChildren } from 'react';
type CardProps = PropsWithChildren<{
variant: 'light' | 'dark';
}>;Polymorphic ComponentAllow callers to customize the underlying HTML tag.
type BoxProps = { as?: 'div' | 'section' };Tips
- Treat all incoming
propsas strictly read-only values to maintain unidirectional data flow across your component hierarchy. - Use TypeScript
interfacedefinitions to document and enforce component prop contracts directly within your code editor.
Warnings
- Never mutate
propsdirectly inside child components because doing so violates fundamental React rendering purity guarantees. - Avoid excessive prop drilling across multiple nested layers by leveraging component
compositionor context sharing.
In Practice
Build a reusable container component accepting header slots, callbacks, and children.
- Define a TypeScript interface for dialog props and actions.
- Destructure title, children, and close handler props.
- Render modal overlay and container card elements.
- Expose dismiss triggers connected to the callback prop.
interface ModalProps {
title: string;
isOpen: boolean;
onClose: () => void;
children: React.ReactNode;
}
export function Modal(props: ModalProps) {
if (!props.isOpen) return null;
return (
<ModalOverlay onClick={props.onClose}>
<ModalCard onClick={(e) => e.stopPropagation()}>
<CardTitle>{props.title}</CardTitle>
<CardBody>{props.children}</CardBody>
<Button onClick={props.onClose}>Close</Button>
</ModalCard>
</ModalOverlay>
);
}FAQ
No. Props are strictly read-only and immutable. If a child needs to change data, the parent must pass an event callback function that updates state in the parent scope.
The children prop automatically captures whatever content you place between the opening and closing JSX tags of a component, enabling flexible container composition.
Use standard JavaScript ES6 parameter destructuring with default assignment syntax directly in the function arguments list, such as ({ title = 'Default' }).