useInfinite calling getKey with undefined pageIndex and previousPageData beta 9
See original GitHub issueBug report
Description / Observed Behavior
When using beta 9 and calling useSWRInfinite((pageIndex, previousPageData) => ...dostuff)
both parameters are showing up as undefined
.
Expected Behavior
According to the types this should always return a number and the previous data or null.
Repro Steps / Code Example
I’m not sure what part of my code is causing this but I am using a provider.
<SWRConfig
value={{
fetcher: axiosFetcher(client),
cache: memoryCache,
}}
>
export const axiosFetcher =
(client: AxiosInstance) =>
async (url: string): Promise<State<any>> => {
const resp = await client({
method: 'get',
url,
});
return createState(resp.data);
};
export const { cache: memoryCache, mutate } = createCache(new Map());
useSWRInfinite<State<Pagination<Gift>>>(
(pageIndex, previousPageData, ...args: any[]) => {
return getKey(
pageIndex,
previousPageData,
pageSize,
orgName,
campaignID,
search
);
}
);
Additional Context
SWR version. 1.0.0-beta.9 / 1.0.0-beta.8
I’m guessing something is going wrong with the middlewares. I think it’s just not calling the infinite middleware.
My suspicion comes from the fact that if I insert pageIndex = pageIndex ?? 0
into the getKey function it returns data, but that data is not an array. (acting like a normal useSWR call).
Trying to debug is crazy as well since all of the js in the package is minified. I think there was some function in the stack that had the middlewares set to something but I have no idea at what point.
It’s possible that the provider config isn’t being merged properly with the config that is adding the middleware with withMiddleware
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:7 (2 by maintainers)
Version of 1.0.1 same behavior with
useSWRInfinite()
To note this issue is occurring on latest release
v1.0.1
.useSWRInfinite
first arg as a typefunction
in example ofgetKey()
passespageIndex
&previousPageData
asundefined
. Downgrade tov0.5.6
this will fix the issue.You can test this in their codesandbox by changing the version in
package.json
tov1.0.1
then tov0.5.6
then destruct the import.OK, now I know why this is happening and why I cannot reproduce it. Since SWR 1.0.0, you have to import
useSWRInfinite
from'swr/infinite'
instead of'swr'
:It’s mentioned here: https://swr.vercel.app/blog/swr-v1#update-useswrinfinite-imports as well as the docs.