swr icon indicating copy to clipboard operation
swr copied to clipboard

useSWRInfinite Mutate function doesn't accept the same arguments as useSWR Mutate

Open kristiancoulsonplt opened this issue 2 years ago • 1 comments

Bug report

Description / Observed Behavior

The docs (and typescript types!) show that the mutate function returned by the useSWRInfinite hook works the same as the useSWRHook Docs: https://swr.vercel.app/docs/pagination#return-values Types (extends the existing hooks return values): https://github.com/vercel/swr/blob/main/infinite/types.ts#L37

However if you dive into the code they don't work the same way at all https://github.com/vercel/swr/blob/main/src/utils/mutate.ts#L21

https://github.com/vercel/swr/blob/main/infinite/index.ts#L186

I'm assuming that at some point in the past they matched, but more recently the regular hooks mutate got changed, and the types for the inf hook do match the regular one, but when in the code editor, it wasn't able to differentiate that the interface with the new options were invalid

Expected Behavior

I would assume they would work the same, or that the docs/types would be updated to reflect how they currently are.

Repro Steps / Code Example

const { data, mutate } = useSWR('url', fetcher);

// does work
mutate(newData, { revalidate: false });
// does work
mutate(newData, false);
const { data, mutate } = useSWRInfinite('url', fetcher);

// doesn't work
mutate(newData, { revalidate: false });
// does work
mutate(newData, false);

Additional Context

Currently on the latest version, it took me so long to work out why this was happening, it kept returning to the old cache without any errors or anything

kristiancoulsonplt avatar Mar 31 '22 14:03 kristiancoulsonplt

Tied to this - a problem from this is there's no way of doing an optimisticUpdate when using useInfiniteSwr

myxoh avatar Jul 28 '22 11:07 myxoh

@shuding Should we close this?

rapiz1 avatar Oct 24 '22 14:10 rapiz1

OK I just realized this has not been released and I got bitten by this

rapiz1 avatar Oct 24 '22 14:10 rapiz1

Even on main, swr/infinite doesn't pass all of the mutate options to the underlying mutate, including optimisticData, populateCache, and rollbackOnError.

E.g., https://github.com/vercel/swr/blob/3606785557ef75eb332c15f2918155be9802d46c/infinite/index.ts#L225

This is exacerbated by the fact that these two versions of mutate have the same TS signature, but don't respond to options the same way.

transitive-bullshit avatar Nov 23 '22 23:11 transitive-bullshit