tsx-modular-component-pattern
Guide for outputting React/TSX components by prioritizing reusable sub-components, controlling length, and declaring them as const before the main component to ensure clear structure and maintainability.
When & Why to Use This Skill
This Claude skill provides a structured framework for generating modular React and TSX components. By enforcing logical decomposition, strict line limits, and a 'decompose-then-compose' architecture, it ensures that generated code is highly maintainable, readable, and follows modern frontend best practices. It prioritizes the declaration of reusable sub-components before the main component to create a clear, predictable dependency order.
Use Cases
- Refactoring complex, monolithic React components into smaller, single-responsibility sub-components to improve maintainability and readability.
- Standardizing frontend codebases by enforcing a consistent pattern where sub-components are declared as constants before the main orchestrator component.
- Accelerating the development of UI libraries by ensuring every component is built with reusability and clear prop-based data flow from the start.
- Improving code review efficiency by maintaining a strict 30-40 line limit for UI modules, making logic easier to audit and test.
| name | tsx-modular-component-pattern |
|---|---|
| description | Guide for outputting React/TSX components by prioritizing reusable sub-components, controlling length, and declaring them as const before the main component to ensure clear structure and maintainability. |
Component Structure
When generating React/TSX components, follow these structural points to maintain information focus under Primacy/Recency:
- Decompose logically related UI modules into independent sub-components, ensuring each handles a single responsibility.
- Declare all sub-components as
constbefore the main component, making dependency order clear at a glance. - Keep each sub-component within 30-40 lines, continue decomposing to finer granularity if needed.
- Use semantic naming (e.g.,
VideoOverlay,ActionButton,CreatorInfo) for quick retrieval and reuse. - Use Props to clarify data and event flow, let main component only handle assembly and state dispatch.
- Regularly review if sub-components can be extracted to shared directory to avoid duplicate implementations.
Reference example:
// Sub-components defined first
const PlayOverlay = ({ isPlaying, onToggle }: PlayOverlayProps) => (
<div className="...">...</div>
)
const ActionButtons = ({ likes, comments }: ActionButtonsProps) => (
<div className="...">...</div>
)
// Main component uses sub-components
export function VideoFeed({ videos }: VideoFeedProps) {
return (
<div>
{videos.map(v => (
<PlayOverlay ... />
<ActionButtons ... />
))}
</div>
)
}
Final confirmation: Main component handles orchestration, sub-components handle details, achieving a stable pipeline of "decompose first, then compose".