You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, in the following code, prev refers to the previous state, not the optimistic state. So when running this twice in a row, prev.quantity remains 0 for both runs, and after the last fetch function resolves, it updates to 1.
As a result, if run multiple times in quick succession, the value only increases once:
You're right. In my example, I use optimistic updates for the "Add to Cart" button, and it causes issues when trying to increase the quantity multiple times. While I can revalidate when an error occurs, the problem is that after the first request resolves, the state updates to a lower quantity than expected when clicking multiple times.
If this behavior is intentional, it would be helpful to explicitly mention it in the documentation. It took me a while to figure out because I was also working with several other libraries and initially thought the issue was coming from them, which led to a lot of wasted time.
If this isn't planned to be change, please close this issue.
Bug report
Description / Observed Behavior
below code work fine
However, in the following code, prev refers to the previous state, not the optimistic state. So when running this twice in a row, prev.quantity remains 0 for both runs, and after the last fetch function resolves, it updates to 1.
As a result, if run multiple times in quick succession, the value only increases once:
This problem is the same in
global mutate
andmutate
from hook.Expected Behavior
When optimistic data increases the value, the next update should reflect the increased value, not the initial state.
Repro Steps / Code Example
Click on "with prev" multiple times optimistic update not work till fetch resolve:
CodeSandbox
Additional Context
SWR version: "swr": "^2.2.5", "^2.3.2"
Next.js version: "next": "14", "15"
The text was updated successfully, but these errors were encountered: