Reverts to initialData when key is reset to null #1250
Unanswered
GriffinSauce
asked this question in
Q&A
Replies: 1 comment
-
Would something like this work? Not sure if const { data } = useSWR(
shouldFetch ? `/api/things` : null,
apiFetch,
{
initialData: shouldFetch ? dataFromSSR : null
},
) |
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
-
When passing initialData and using conditional fetching the data will reset to initialData when the condition is set to falsy instead of leaving it on the last-fetched data.
Example:
shouldFetch
to true -> will fetch fresh datashouldFetch
to false -> data reverts to initialData instead of staying "fresh"I've created a workaround that displays the behaviour I expected:
shouldFetch
to true -> will fetch fresh datashouldFetch
to false -> last fetched data will be returnedI appreciate that resetting data to
null
when noinitialData
is passed makes sense, so I'm not sure what the best solution is but the current behaviour is a bit whacky with this combo of settings.Beta Was this translation helpful? Give feedback.
All reactions