Prefer Composition Over Monolithic Conditional Rendering
Share
Monolithic components with deeply nested ternaries and conditionals are hard to read, test, and extend. Use composition patterns (children, render props, compound components).
Why This Matters
improves readability, testability, and extensibility of component APIs
Prefer Composition Over Monolithic Conditional Rendering
Impact: MEDIUM (improves readability, testability, and extensibility of component APIs)
When a single component handles many variants through deeply nested ternaries, long chains of if/else, or numerous boolean props (isLoading, isEmpty, isError, isCompact, isAdmin), it becomes a "god component" that is difficult to understand, test, and extend. Each new variant adds more branching, increasing cyclomatic complexity. Composition patterns — children, render props, compound components — distribute responsibility across focused, testable units.
Compound components (like <Tabs>, <Tabs.Panel>, <Tabs.List>) are the gold standard for complex UI components. They share state implicitly via context while exposing a flexible, composable API.
The children prop is the simplest form of composition and should be the first approach considered.
When a component has more than 3 boolean variant props, it is a strong signal to refactor into composed sub-components.
Detection hints:
# Find deeply nested ternaries in JSXgrep -rn "? <" src/ --include="*.tsx" --include="*.jsx"# Find components with many boolean propsgrep -rn "isLoading\|isEmpty\|isError\|isCompact\|isDisabled" src/ --include="*.tsx"