Composition and inheritance are two ways to build components by reusing code. Composition means putting smaller parts together. Inheritance means creating a new component based on an existing one.
Composition vs inheritance in NextJS
/* Composition example */ function Button({ children }) { return <button>{children}</button>; } function Dialog() { return ( <div> <h2>Title</h2> <Button>Click me</Button> </div> ); } /* Inheritance example (not common in React/Next.js) */ import React from 'react'; class BaseButton extends React.Component { render() { return <button>{this.props.label}</button>; } } class SpecialButton extends BaseButton { render() { return <button style={{ color: 'red' }}>{this.props.label}</button>; } }
In Next.js and React, composition is preferred over inheritance.
Composition uses props and children to combine components.
function WelcomeMessage({ name }) { return <p>Welcome, {name}!</p>; } function Page() { return ( <div> <WelcomeMessage name="Alice" /> </div> ); }
import React from 'react'; class Button extends React.Component { render() { return <button>{this.props.label}</button>; } } class DangerButton extends Button { render() { return <button style={{ backgroundColor: 'red' }}>{this.props.label}</button>; } }
This example shows composition by building UserInfo from Avatar and user data. It also shows inheritance by creating RedButton from Button. In Next.js, composition is the recommended way.
import React from 'react'; // Composition: Small reusable components function Avatar({ user }) { return <img src={user.avatarUrl} alt={`${user.name} avatar`} style={{ width: '50px', borderRadius: '50%' }} />; } function UserInfo({ user }) { return ( <div style={{ display: 'flex', alignItems: 'center', gap: '1rem' }}> <Avatar user={user} /> <div>{user.name}</div> </div> ); } // Inheritance example (rare in React, shown for comparison) class Button extends React.Component { render() { return <button>{this.props.label}</button>; } } class RedButton extends Button { render() { return <button style={{ color: 'white', backgroundColor: 'red' }}>{this.props.label}</button>; } } export default function App() { const user = { name: 'Jane Doe', avatarUrl: 'https://via.placeholder.com/50' }; return ( <div style={{ padding: '1rem', fontFamily: 'Arial, sans-serif' }}> <h1>User Profile</h1> <UserInfo user={user} /> <div style={{ marginTop: '1rem' }}> <Button label="Normal Button" /> <RedButton label="Red Button" /> </div> </div> ); }
React and Next.js favor composition because it keeps components simple and flexible.
Inheritance can make code harder to follow and is rarely used in React.
Use composition to build UI like Lego blocks, combining small pieces into bigger ones.
Composition means building components by combining smaller parts.
Inheritance means creating new components by extending existing ones.
In Next.js, prefer composition for clearer and easier code.
Practice
composition over inheritance when building components?Solution
Step 1: Understand composition in Next.js
Composition means building components by combining smaller, reusable parts to create complex UI.Step 2: Compare with inheritance
Inheritance extends existing components but can lead to complex and less clear code structures.Final Answer:
It allows combining small components for better reuse and clarity. -> Option AQuick Check:
Composition = combining parts for clarity [OK]
- Thinking inheritance is simpler for UI reuse
- Confusing lifecycle management with composition
- Assuming inheritance is preferred in Next.js
Solution
Step 1: Identify JSX syntax for composition
Composition uses JSX tags to include child components inside parent components.Step 2: Check options for correct JSX usage
function Parent() { return <Child></Child>; }correctly uses<Child></Child>tags, which is valid JSX.function Parent() { return <Child>; }uses opening tag but missing closing tag.class Parent extends Child {}uses inheritance syntax, not composition.function Parent() { return Child(); }calls Child as a function, which is not standard JSX.Final Answer:
<Child></Child> JSX tags for composition -> Option BQuick Check:
JSX tags = composition [OK]
- Using class extends syntax for composition
- Calling components as plain functions without JSX
- Missing closing tag for JSX element
function Button({ label }) {
return <button>{label}</button>;
}
function Toolbar() {
return <div><Button label="Save" /></div>;
}Solution
Step 1: Analyze Button component output
The Button component returns a button element with the label text inside it.Step 2: Analyze Toolbar component output
Toolbar renders a div containing the Button component with label "Save", so the button with text "Save" appears inside the div.Final Answer:
<button>Save</button> -> Option AQuick Check:
Button label = Save [OK]
- Confusing parent div content with button content
- Assuming Toolbar text appears inside button
- Thinking syntax error exists due to JSX
class BaseButton extends React.Component {
render() {
return <button>Base</button>;
}
}
function SubmitButton() {
return <BaseButton label="Submit" />;
}Solution
Step 1: Check BaseButton usage of props
BaseButton receives a label prop but does not use it in the render method, so label is ignored.Step 2: Check SubmitButton usage
SubmitButton returns BaseButton with label prop, but since BaseButton ignores label, the button always shows "Base".Final Answer:
BaseButton does not use the label prop in render method. -> Option DQuick Check:
Unused prop = bug [OK]
- Thinking functional components can't use class components
- Assuming inheritance is disallowed in Next.js
- Confusing extending with composing
function Card({ children }) {
return <div className="card">{children}</div>;
}
function Profile() {
return (
<Card>
<h2>User Name</h2>
<p>User details here</p>
</Card>
);
}Solution
Step 1: Understand composition with children prop
The children prop allows passing any nested JSX inside Card, making it reusable for different content.Step 2: Compare other options
Extending with inheritance is less flexible and not recommended. Passing content as string limits flexibility. Duplicating code reduces reuse.Final Answer:
Use children prop to pass nested components inside Card. -> Option CQuick Check:
children prop = composition [OK]
- Trying to extend components with class inheritance
- Passing content as plain strings instead of JSX
- Duplicating component code instead of composing
Ready to prove it?
Pass the test and this pattern moves unknown
