Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Let useRef infer its type instead of annotating a written ref as RefObject
Match ref mutability to the installed React types and ownership; RefObject is not universally readonly.
Bad example
| 1 | const portfoliosRef: RefObject<Portfolio[]> = useRef(portfolios); |
| 2 | portfoliosRef.current = portfolios; // RefObject.current is readonly |
Explanation (EN)
A readonly annotation in older React types conflicts with an intended owned mutable ref.
Objašnjenje (HR)
Good example
| 1 | const portfoliosRef = useRef(portfolios); |
| 2 | useEffect(() => { |
| 3 | portfoliosRef.current = portfolios; |
| 4 | }, [portfolios]); |
Explanation (EN)
This owned ref updates after commit; verify whether consumers require earlier commit timing.
Objašnjenje (HR)
Notes (EN)
Use a ref type that reflects whether the installed React types permit writes and whether the component owns that ref.
Exceptions / Tradeoffs (EN)
React 19 types make RefObject.current mutable; earlier versions differ. Do not mutate ref.current during render merely to synchronize changing props; use the correct commit lifecycle.