Typing Props and Children
On this page
Typing Children Correctly
- Use React.ReactNode for generic renderable content.
- Use stricter types only when enforcing structure.
- Do not implicitly accept children unless required.
Example: Generic Container
type CardProps = {
title: string;
children: React.ReactNode;
};
function Card({ title, children }: CardProps) {
return (
<section>
<h3>{title}</h3>
{children}
</section>
);
}
Example: Render Prop
type ListProps<T> = {
items: T[];
render: (item: T) => React.ReactNode;
};
function List<T>({ items, render }: ListProps<T>) {
return <>{items.map(render)}</>;
}
Common Mistakes
- Accepting children when component does not use them.
- Using any for children.
- Over restricting children and blocking valid use cases.
Failure Modes
- Unexpected layout injection from implicit children.
- Hard to reuse components due to overly strict types.
- Runtime crashes from unsafe assumptions about children shape.
Checklist
- Children explicitly typed only when needed.
- No implicit React.FC children behavior.
- Render props typed explicitly.