[RFC] Less misleading useSWR signature
#2786
SevenOutman
started this conversation in
RFC
Replies: 1 comment 2 replies
-
|
Cloud go deeper in the reason why there are some situations that may use the same key but different fetcher? In my opinion, SWR requires you claim all dependency clearly in the key to match specific fetcher, and it make efforts to develop a serialization method to achieve this. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
The problem
Current signature of
useSWR(key, fetcher)allows user to definekeyandfetcherassociations peruseSWRcall, which can be misleading and error-prone.Assume we call
useSWRwith the samekeyand differentfetchers like this (as the signature allows):The data in Component A can be either
dataAordataBdepending on how Component B is used, vice versa. This makes thefetcherarg inuseSWRmeaningless, because it does not reflect what'sdataactually going to be.Deeper reason
useSWRdata, a resource identified by a givenkeyshould have only one fetcher associated, which defines how that resource should be populated in the cacheuseSWRis not the place for defining this association, but for declaring a subscription to a resource in the cache identified by the givenkey. Unlike the one-and-only fetcher for a resource, there can be multiple different subscriptions at the same time.This one-to-one-to-many relation (fetcher-cache-subscriptions) bears this fixed conclusion - defining fetcher vs subscriptions are two separate things.
Current signature
useSWR(key, fetcher)brings the convenience of not having to declare the fetcher separately but right when it comes to you that you need to read a resource. But it also brings ambiguity in the long run - it's less obvious to the user thatuseSWRis not registering the fetcher (well, it currently is but it should not be) but creating a subscription to the resource in the cache.This proposal
The core of this proposal is to disallow the anti-pattern of defining
key-fetcherassociation peruseSWRcall. There're two approaches accordingly:Approach 1: only allow key at
useSWRcall and define fetcher elsewhereFirst,
useSWRshould not allow to definekey-fetcherassociation in-place, but only accept arguments around which resource to subscribe and how the subscription is fed.Second, define the
key-fetcherassociation globally or per cache instance. This can mostly be done with a global fetcher.With this approach, there would be few more steps to kick-off swr in a project than what swr is offering now, but we already know that splitting app state management from the component tree is a laborious (yet good) practice to adopt.
Approach 2: fetcher as key
Allowing passing the fetcher itself along with its arguments as resource key keeps things straight-forward as it is today, while address the problem around key-fetcher associations.
Possible implementation
As we can already serialize an array of generic values as key stably, use a Map/WeakMap to map fetchers to a set of serialized arguments it's called with
This is just a rough idea, please remind me if I'm missing anything.
Beta Was this translation helpful? Give feedback.
All reactions