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 ruleP2stack specificStack: react
reactcontextproviderencapsulation
Encapsulate context state inside a provider component
Wrap context value/state in a dedicated provider component co-located with the context, instead of wiring state and value inside an unrelated consumer component.
PR: hegnar-forum-web · org-mining-hist-2026-06Created: Jun 20, 2026
Bad example
Old codetsx
| 1 | function Discussion() { |
| 2 | const [thread, setThread] = useState(); |
| 3 | const [openDialog, setOpenDialog] = useState(false); |
| 4 | return ( |
| 5 | <DiscussionContext.Provider value={{ thread, setThread, openDialog, setOpenDialog }}> |
| 6 | {children} |
| 7 | </DiscussionContext.Provider> |
| 8 | ); |
| 9 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 1 | function DiscussionProvider({ children }) { |
| 2 | const [thread, setThread] = useState(); |
| 3 | const [openDialog, setOpenDialog] = useState(false); |
| 4 | return ( |
| 5 | <DiscussionContext.Provider value={{ thread, setThread, openDialog, setOpenDialog }}> |
| 6 | {children} |
| 7 | </DiscussionContext.Provider> |
| 8 | ); |
| 9 | } |
Explanation (EN)
Objašnjenje (HR)