Hint
React's composition model: components accept children as props and render them wherever they want. This is more flexible than inheritance and avoids prop drilling.
// Simulates React's children prop pattern
function Card({ title, children }) {
return [Card: ${title}] ${children};
}
function Button({ onClick, children }) {
return [Btn: ${children}];
}
// Composition — Card wraps Button
const result = Card({
title: 'Actions',
children: Button({ children: 'Submit' }) + ' ' + Button({ children: 'Cancel' })
});
console.log(result);
// HOC composes by wrapping
function withBorder(Component) {
return (props) => |${Component(props)}|;
}
const BorderedCard = withBorder(Card);
console.log(BorderedCard({ title: 'Box', children: 'content' }));[Card: Actions] [Btn: Submit] [Btn: Cancel] |[Card: Box] content|
Explanation: children prop passes rendered JSX as data. HOC wraps the component, adding decoration around the output.
Key Insight: React's composition model: components accept children as props and render them wherever they want. This is more flexible than inheritance and avoids prop drilling.