-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: add getInfiniteKey to mutate the cache for useSWRInfinite #1257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit ef31a87:
|
Love this. I got another question, how can we have the global mutate function when we are using the cache provider? |
ccc799c
to
efb1459
Compare
src/index.ts
Outdated
@@ -1,5 +1,5 @@ | |||
// Core APIs | |||
export { SWRConfig, mutate, createCache } from './use-swr' | |||
export { SWRConfig, mutate, createCache, mutateCustomCache } from './use-swr' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if we want to expose the internalMutate here since createCache
has already covered the similar usage
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think it's not the best way to support the use case. We might have to find out another solution like exporting a function that returns a key like INFINITE_PREFIX + getFirstPageKey(getKey)
rather than exporting a mutate funciton.
test/use-swr-infinite.test.tsx
Outdated
await screen.findByText('data:page-test-12-0:2') | ||
|
||
await act(() => | ||
mutateInfinite(index => `page-test-12-${index}`, 'local-mutation', false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
users have the keyLoader
definition, at the same time they also have the createCache
to get the mutate
function, then combining both of them can also achieve the similar approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean like mutate(getInfiniteKey(getKey), 'local-mutation', false)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, that would be nice!
That concern makes sense. I've added But I feel this is not the best way for the feature. Another solution is #1257 (comment) |
a1ce233
to
8e6afc9
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! I think this is the best choice for now until we have a better way to work around custom cache providers.
Thank you!🚀 |
@shuding could you please release this? |
@nicholaschiang A new beta released is there, please check! We will document new APIs when it's stable. |
@shuding this doesn't seem to support mutating based on current data. It's not included in your tests and it's not working here: // I have an updated message and I need to mutate the list of messages.
const message = { ...prev, subject: 'This is an updated value.' };
await mutate(
getInfiniteKey(getKey),
(data?: MessagesRes[]) => data?.map((messages: MessageRes) => {
// `data` is always `undefined` even when I know the list has loaded.
const idx = messages.findIndex((m) => m.id === message.id);
if (idx < 0) return messages;
return [...messages.slice(0, idx), message, ...messages.slice(idx + 1)];
}),
false
); |
@nicholaschiang I've made a PR to confirm your case. |
fixes #1156
This PR adds a new API calledmutateInfinite
, which is a mutation function foruseSWRInfinite
and based on #1156 (comment).This PR adds a new API called
getInfiniteKey
to get a key to mutate cache data foruseSWRInfinite
, which receives a key loader function and returns the key for the cache data.I feel this is still in discussion and have not meet an agreement, so this is just a POC.