Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
frontend ruleP2universalStack: typescript
reactdesignlookup-mapvariants
Select behavior by variant with a lookup map
When a component must choose between known implementations, drive the choice with a variant prop mapped through a record rather than branching logic.
PR: hegnar-forum-web · org-mining-hist-2026-06Created: Jun 20, 2026
Bad example
Old codetsx
| 1 | const makeFetcher = (variant: string) => |
| 2 | async (page: number, size: number) => { |
| 3 | if (variant === 'ticker') return getTickerThreads(page, size); |
| 4 | if (variant === 'category') return getCategoryThreads(page, size); |
| 5 | }; |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 1 | type Fetcher = (page: number, size: number) => Promise<ThreadList>; |
| 2 | type Variant = 'ticker' | 'category'; |
| 3 |
|
| 4 | const fetcherByVariant: Record<Variant, Fetcher> = { |
| 5 | ticker: getTickerThreads, |
| 6 | category: getCategoryThreads, |
| 7 | }; |
| 8 |
|
| 9 | const List = ({ variant }: { variant: Variant }) => { |
| 10 | const fetcher = fetcherByVariant[variant]; |
| 11 | // ... |
| 12 | }; |
Explanation (EN)
Objašnjenje (HR)