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 ruleP2universalStack: javascript
callbacksdefensiveapi-design
Make optional callbacks safe with a default or optional call
When a callback prop is optional, either default it to a no-op or invoke it with optional chaining (callback?.()) so callers aren't forced to pass an empty function.
PR: hegnar-forum-web · org-mining-hist-2026-06Created: Jun 20, 2026
Bad example
Old codets
| 1 | const useInfiniteScroll = ({ callback }) => { |
| 2 | // throws if a caller didn't pass callback |
| 3 | callback({ currentPage: page + 1, isInfiniteScroll: true }); |
| 4 | }; |
Explanation (EN)
Objašnjenje (HR)
Good example
New codets
| 1 | const useInfiniteScroll = ({ callback }) => { |
| 2 | callback?.({ currentPage: page + 1, isInfiniteScroll: true }); |
| 3 | }; |
| 4 | // or: ({ callback = () => {} }) => { callback(...); } |
Explanation (EN)
Objašnjenje (HR)