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 ruleP1stack specificStack: react
reactcontextcorrectness
Use null as createContext default so the missing-provider guard works
Default a context to null so a !context check can actually catch usage outside its provider instead of being dead code.
PR: hegnar-forum-web · org-mining-deep-2026-06Created: Jun 17, 2026
Bad example
Old codets
| 1 | const Ctx = createContext(defaultObject); |
| 2 | if (!ctx) console.error('missing provider'); // never runs |
Explanation (EN)
Objašnjenje (HR)
Good example
New codets
| 1 | const Ctx = createContext<Value | null>(null); |
| 2 | if (!ctx) throw new Error('useX must be used within provider'); |
Explanation (EN)
Objašnjenje (HR)