Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Conditionally render a component rather than mounting it to return null
When a component only renders for a non-null state, render it conditionally from the parent instead of always mounting it and returning null.
Bad example
| 1 | function Modal({ activeAction }) { |
| 2 | if (!activeAction) return null; // always mounted |
| 3 | return <Dialog ... />; |
| 4 | } |
| 5 | <Modal activeAction={activeAction} /> |
Explanation (EN)
An always-mounted child can carry avoidable nullable branches when no inactive lifecycle is needed.
Objašnjenje (HR)
Good example
| 1 | {activeAction && <Modal activeAction={activeAction} />} |
Explanation (EN)
Objašnjenje (HR)
Notes (EN)
Move a visibility condition to the parent when the child should truly unmount while inactive and its props can become a simpler non-null contract.
Exceptions / Tradeoffs (EN)
Returning null keeps component state/effects alive, while conditional mounting resets them. Preserve drafts, subscriptions and animation lifecycle when those are intentional.