React component building and composition best practices. Use when creating, reviewing, or refactoring React components. Covers component structure, props pat...
Best practices for building reusable, maintainable React components.
Reference these guidelines when:
Each component should do one thing well. Split large components into smaller, focused pieces.
Prefer composing components together rather than complex inheritance hierarchies.
// ✅ Good: Composition
function Page() {
return (
<Layout>
<Header />
<Main>
<Article />
</Main>
<Footer />
</Layout>
);
}
// ❌ Avoid: Deep nesting
function Page() {
return <LayoutWithHeaderAndFooter><MainContent /></LayoutWithHeaderAndFooter>;
}
// ✅ Recommended structure
import { FC } from 'react';
interface Props {
title: string;
children?: React.ReactNode;
}
export const Card: FC<Props> = ({ title, children }) => {
return (
<div className="card">
<h2>{title}</h2>
{children}
</div>
);
};
For flexible APIs like Select/Option, Tabs/TabList/Tab/TabPanel.
For sharing behavior while keeping rendering control.
For sharing stateful logic across components.
ZIP package — ready to use