Concept Flow - Composition vs inheritance
Start
Choose approach
Use Inheritance
Extend base class
Override or add
Use in app
End
Shows the decision flow between using inheritance or composition in Next.js components.
function Button(props) { return <button>{props.label}</button>; } function IconButton(props) { return <Button label={props.label} />; }
| Step | Component Rendered | Props Passed | Output Rendered | Notes |
|---|---|---|---|---|
| 1 | Button | { label: 'Click me' } | <button>Click me</button> | Button renders with label from props |
| 2 | IconButton | { label: 'Click me' } | <button>Click me</button> | IconButton composes Button, passing label |
| 3 | App uses IconButton | N/A | <button>Click me</button> | IconButton used in app, renders Button inside |
| 4 | End | N/A | N/A | Rendering complete |
| Variable | Start | After IconButton Render | Final |
|---|---|---|---|
| props.label | undefined | 'Click me' | 'Click me' |
Composition vs Inheritance in Next.js: - Inheritance: Extend a base component class (rare in React/Next.js) - Composition: Build components by combining others via props/children - Composition is preferred for flexibility and reuse - Pass props to child components to customize - Avoid deep inheritance chains in React apps