[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
-
The problem
Current signature of
useSWR(key, fetcher)
allows user to definekey
andfetcher
associations peruseSWR
call, which can be misleading and error-prone.Assume we call
useSWR
with the samekey
and differentfetcher
s like this (as the signature allows):The data in Component A can be either
dataA
ordataB
depending on how Component B is used, vice versa. This makes thefetcher
arg inuseSWR
meaningless, because it does not reflect what'sdata
actually going to be.Deeper reason
useSWR
data, a resource identified by a givenkey
should have only one fetcher associated, which defines how that resource should be populated in the cacheuseSWR
is 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 thatuseSWR
is 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
-fetcher
association peruseSWR
call. There're two approaches accordingly:Approach 1: only allow key at
useSWR
call and define fetcher elsewhereFirst,
useSWR
should not allow to definekey
-fetcher
association in-place, but only accept arguments around which resource to subscribe and how the subscription is fed.Second, define the
key
-fetcher
association 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