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
reactstateperformancereadability
Update only the changed item instead of recomputing the whole collection
When changing one element of an array in a state update, copy and splice that single index rather than mapping every item and computing values you only need for one.
PR: hegnar-forum-web · org-mining-hist-2026-06Created: Jun 20, 2026
Bad example
Old codets
| 1 | setValue(prev => prev.map((item, i) => { |
| 2 | const calculatedField = { /* computed for every item */ }; |
| 3 | return index === i ? { ...item, calculatedField } : item; |
| 4 | })); |
Explanation (EN)
Objašnjenje (HR)
Good example
New codets
| 1 | setValue(prev => { |
| 2 | const next = [...prev]; |
| 3 | const item = next[index]; |
| 4 | next.splice(index, 1, { ...item, calculatedField: buildField(item) }); |
| 5 | return next; |
| 6 | }); |
Explanation (EN)
Objašnjenje (HR)