Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Dedupe with a Set and spread back to an array for readability
To add a unique item to a list, build a Set and spread it to an array rather than hand-rolling index checks.
Bad example
| 1 | if (this.state.visited.indexOf(url) === -1) { |
| 2 | this.setState({ visited: this.state.visited.concat(url) }); |
| 3 | } |
Explanation (EN)
Manual membership updates based on an old state snapshot can lose a concurrent addition.
Objašnjenje (HR)
Good example
| 1 | this.setState(previous => { |
| 2 | if (previous.visited.includes(url)) return null; |
| 3 | return { visited: [...new Set(previous.visited).add(url)] }; |
| 4 | }); |
Explanation (EN)
The class-state updater reads current state and preserves existing insertion order while avoiding a redundant update for an existing URL.
Objašnjenje (HR)
Notes (EN)
Use Set for membership deduplication when SameValueZero equality and insertion order match the domain; update from the current state.
Exceptions / Tradeoffs (EN)
Objects deduplicate by identity, not fields. If nothing changes, preserve the previous state reference where useful. A set does not solve stale reads from this.state outside a functional update.