Replies: 1 comment
-
@nevnein This seems to be hard to fix this in flushSync(() => setCalls((prev) => [...prev, list[prev.length]]));
setSize((size) => size + 1); |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Bug report
Description / Observed Behavior
I was trying to make multiple dynamic calls using useSWRInfinite as suggested here: my idea was to keep track of the dynamic calls inside an array stored in a
useState
, and then in thegetKey
simply access the array by index and use the info to make the correct fetch. To trigger the calls, I manually callsetSize
when the array changes.Here's some simple code
It turns out that
getKey
accesses the previous state ofcalls
: if we have 2 calls and add another one,getKey
will be correctly called 3 times butcalls[2]
will beundefined
.Expected Behavior
I expect the
calls
in thegetKey
function to be fresh, and correctly hold the current state.Repro Steps / Code Example
Here's the CodeSandbox. You can see in the console that the value of
calls[index]
isundefined
.Additional Context
SWR version: 1.2.2
I know I should handle multiple calls inside multiple components, but in my case I need to make multiple calls depending on arbitrary user interaction and feed the data directly into only one component (a chart).
Beta Was this translation helpful? Give feedback.
All reactions