Reusable Component Patterns
In this page:
function ReusableComponent({ variant, children }) {
return <element className={variant}>{children}</element>;
}
<ReusableComponent variant="value">content</ReusableComponent>
Identifying Reuse Opportunities
When you notice the same JSX pattern appearing in multiple places with only small differences, that's a sign it should become a reusable component.
Extracting it once and passing in the differences as props avoids duplicating the same markup everywhere.
Example: Identifying Reuse Opportunities
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function Badge({ label, color }) {
return <span style={{background: color, padding: "2px 8px", borderRadius: "4px"}}>{label}</span>;
}
function App() {
return (
<div>
<Badge label="New" color="lightgreen" />
<Badge label="Sale" color="orange" />
</div>
);
}
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
</script>
</body>
</html>
Parameterizing with Props
To make a component genuinely reusable, identify what varies between its use cases and turn those into props. This keeps the component's internal structure fixed and consistent while letting each usage customize exactly what it needs.
Example: Parameterizing with Props
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function Alert({ message, type }) {
const colors = { error: "red", success: "green", info: "blue" };
return <div style={{color: colors[type], border: `1px solid ${colors[type]}`, padding: "8px"}}>{message}</div>;
}
function App() {
return (
<div>
<Alert message="Something went wrong" type="error" />
<Alert message="Saved successfully" type="success" />
</div>
);
}
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
</script>
</body>
</html>
Balancing Flexibility and Simplicity
A reusable component should be flexible enough to cover real variations, but not so configurable that it becomes confusing to use. A good sign of balance: most callers only need to pass one or two props, with the rest relying on sensible defaults.
Example: Balancing Flexibility and Simplicity
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
function Tag({ text, color = "gray" }) {
return <span style={{background: color, color: "white", padding: "2px 6px", borderRadius: "3px"}}>{text}</span>;
}
function App() {
return <div><Tag text="Default" /><Tag text="Urgent" color="red" /></div>;
}
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
</script>
</body>
</html>
- Hardcoding specific text or values inside a component instead of exposing them as props.
- Copy-pasting a component's JSX to make a slightly different version instead of parameterizing it.
- Making a component too rigid by not allowing any customization via props.
- Reusable components are designed to work in multiple places with different data.
- Props are the main tool for making a component configurable and reusable.
- Well-designed reusable components reduce code duplication across an app.
- A good reusable component balances flexibility with simplicity.
No browser-specific restrictions -- this is a component design practice, not a runtime feature.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: