-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Hook for triggering validations manually (passive mode) #1262
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 be647cc:
|
trigger/index.ts
Outdated
if (!fetcher) throw new Error('Missing SWR fetcher.') | ||
|
||
// Mutate the SWR data and return the result. | ||
return swr.mutate( |
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.
RE:
Sometimes, mutation APIs (POST, PUT, DELETE) return different payload from the GET API. It might be an issue if we write back to the cache with the same key. This behavior needs to be reconsidered.
When the user is not interested in updating the global cache for key
they can use the fetcher
right away i.e. they won't need useSWRTrigger
right?
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. But I'm also thinking about an option to prevent updating the cache, but then useSWRTrigger
will have nothing "SWR". Haven’t decided yet.
This API needs more discussions, will keep it on hold for 1.0. |
Some thoughts:
|
The solution that I am currently using (https://twitter.com/giuseppegurgone/status/1427576808204034061) is close to this except that I don't use a key and revalidate but I think that triggering a revalidation (a GET request) when the remote mutation is complete is a good option although it could be redundant work, lead to higher consumption of resources since the mutation could already return the fresh data. |
Continuing this feature in #1450. |
Motivation
There are a lot of existing discussions and feature requests about doing requests manually in SWR (e.g. #93, #116, #550, #1082), and we do see some valid use cases for it. In short, we need something that:
*: Potentially we can have a switch to turn on/off this step.
One option is to have a new hook, because the arguments would be slightly different from the
useSWR
hook. Since we now have better abstractions to build such a hook, namelymiddlewares
andrevalidateWhenStale
, this PR is such an attempt.useSWRTrigger
The
trigger()
call will still reflect the hook-returneddata
anderror
with a re-render, but that's more declarative.To-dos
useSWR
hook with the same key. This can be another option to consider.isValidating
state are working as expected.trigger
prevents race conditions with normal GET requests.useSWRTrigger
and ignore the others.