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 ruleP1stack specificStack: react
reactstateconstructorscorrectness
Initialize state by assignment in the constructor, never via setState
Set the initial state with a direct this.state assignment; calling setState in the constructor is a no-op for the first render and is incorrect.
PR: frontpage-web · org-mining-hist-2026-06Created: Jun 18, 2026
Bad example
Old codetsx
| 1 | constructor(props) { |
| 2 | super(props); |
| 3 | this.setState({ width: 0 }); |
| 4 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetsx
| 1 | constructor(props) { |
| 2 | super(props); |
| 3 | this.state = { width: 0 }; |
| 4 | } |
Explanation (EN)
Objašnjenje (HR)