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.
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.
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
useSWRhook. Since we now have better abstractions to build such a hook, namelymiddlewaresandrevalidateWhenStale, this PR is such an attempt.useSWRTriggerThe
trigger()call will still reflect the hook-returneddataanderrorwith a re-render, but that's more declarative.To-dos
useSWRhook with the same key. This can be another option to consider.isValidatingstate are working as expected.triggerprevents race conditions with normal GET requests.useSWRTriggerand ignore the others.