Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Don't spread into a new object just to alias a value
If you're not adding or overriding any fields, assign the value directly instead of spreading it into a new object literal.
Bad example
| 1 | const data = { |
| 2 | ...useRouteLoaderData<typeof loader>('route')!, |
| 3 | }; |
Explanation (EN)
The spread copies every field into a new object without changing anything, adding an allocation and obscuring that data is just the loader result.
Objašnjenje (HR)
Spread kopira svako polje u novi objekt bez ikakve promjene, dodajuci alokaciju i skrivajuci da je data samo rezultat loadera.
Good example
| 1 | const data = useRouteLoaderData<typeof loader>('route')!; |
Explanation (EN)
The value is aliased directly, which is clearer and avoids the needless copy.
Objašnjenje (HR)
Vrijednost se izravno aliasira, sto je jasnije i izbjegava nepotrebno kopiranje.
Exceptions / Tradeoffs (EN)
Spreading is correct when you add, override, or omit fields, or when you intentionally need a shallow copy to avoid mutating the source.
Iznimke / Tradeoffi (HR)
Spread je ispravan kad dodajes, prepisujes ili izostavljas polja, ili kad namjerno trebas plitku kopiju da ne mutiras izvor.