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
I'm using SWR to update AsyncStorage. I am calling the same key two times quickly in a row, and even though I'm using optimisticData, I keep getting from the calls results parting from the original state. To illustrate:
What I want to achieve is:
Original state: { value: 1}
State after call #1 (add 1): { value: 2}
State after call #2 (subtract 1): { value: 1}
Instead, what I get is:
Original state: { value: 1}
State after call #1 (add 1): { value: 2}
State after call #2 (subtract 1): { value: 0}
And this last result is what gets persisted in AsyncStorage. This happens because both calls, being called in a quick succession, part from the same original state { value : 1} instead of the optimisticData kicking in and feeding the local new state to the next call.
Why is this happening? Is there a way I can get SWR to work the way I want? Here is my code:
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using
SWR
to updateAsyncStorage
. I am calling the same key two times quickly in a row, and even though I'm usingoptimisticData
, I keep getting from the calls results parting from the original state. To illustrate:What I want to achieve is:
Instead, what I get is:
And this last result is what gets persisted in
AsyncStorage
. This happens because both calls, being called in a quick succession, part from the same original state{ value : 1}
instead of theoptimisticData
kicking in and feeding the local new state to the next call.Why is this happening? Is there a way I can get SWR to work the way I want? Here is my code:
I'm using:
swr 1.3.0
,react-native 0.64.3
,react-native-async-storage/async-storage ~1.15.0
Component doing the calls:
Custom SWR hook:
Fetcher fed to SWRConfig:
Beta Was this translation helpful? Give feedback.
All reactions