MediumComponent Patterns💻 Output Question

Children prop — components as data

💡

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.

What does this output?

// 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' }));

Correct Output

[Card: Actions] [Btn: Submit] [Btn: Cancel]
|[Card: Box] content|

Why this output?

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.

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz