fix: use functional setState to avoid stale closures in show-billing#3778
fix: use functional setState to avoid stale closures in show-billing#3778karyanayandi wants to merge 1 commit intoDokploy:canaryfrom
Conversation
Direct reads of serverQuantity inside onClick handlers can produce stale values in closures. Use the callback form setState(prev => ...) so React always supplies the latest state value.
| @@ -1082,7 +1082,9 @@ export const ShowBilling = () => { | |||
| onClick={() => { | |||
| if (serverQuantity <= 1) return; | |||
There was a problem hiding this comment.
this check still uses the stale serverQuantity value from the closure - if you're fixing stale closures, this line should be removed since the functional setState now handles the boundary check
| if (serverQuantity <= 1) return; |
There was a problem hiding this comment.
Pull request overview
This PR fixes stale closure issues in the billing component's server quantity controls by converting direct state reads to functional setState callbacks. The changes specifically target the "legacy" pricing tier's increment and decrement handlers.
Changes:
- Updated the decrement handler to use functional setState with prev parameter
- Updated the increment handler to use functional setState with prev parameter
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (serverQuantity <= 1) return; | ||
|
|
There was a problem hiding this comment.
The guard condition still reads from the stale serverQuantity closure variable instead of using the current state value. This defeats the purpose of the functional setState fix. This line should be removed since the guard is now handled inside the setState callback on line 1086.
| if (serverQuantity <= 1) return; |
| setServerQuantity((prev) => | ||
| prev <= 1 ? prev : prev - 1, | ||
| ); |
There was a problem hiding this comment.
The decrement handler implementation is inconsistent with the existing patterns in the codebase. The "hobby" section (line 731) and "startup" section (line 865) use Math.max() for a cleaner implementation: setServerQuantity((q) => Math.max(1, q - 1)). Consider adopting the same pattern here for consistency.
What is this PR about?
Direct reads of serverQuantity inside onClick handlers can produce stale values in closures. Use the callback form setState(prev => ...) so React always supplies the latest state value.
Checklist
Before submitting this PR, please make sure that:
canarybranch.Issues related (if applicable)
Screenshots (if applicable)
Greptile Summary
Fixed stale closure issue by converting
setServerQuantitycalls to use functional setState pattern withprev =>callback, ensuring React always provides the latest state value.serverQuantity - 1to(prev) => prev <= 1 ? prev : prev - 1serverQuantity + 1to(prev) => prev + 1Confidence Score: 4/5
serverQuantityvalue before calling setState - this check is now redundant since the functional setState handles the boundary condition. The button's disabled attribute already prevents invalid states, making this a low-risk issue.Last reviewed commit: 29083c9
(4/5) You can add custom instructions or style guidelines for the agent here!