Version 2.0 plan
otakustay opened this issue · 2 comments
Explicit naming
useResource
requires a key
parameter, to name each resource explicitly.
This is required as we migrate to a more robust underlying framework like recoil
.
Undecided: maybe scope is another entity should be explicitly named?
Transitions
Since the official useTransition
's release date is unpredictable, we learnt from it that we should treat suspended state as "a state where user is involved in a long wait", for short resource transitions, we need a more fine-grained pending control, that is:
- Provide a
timeout
option before which the hook won't throw aPromise
. - Return a
pending
indicator before suspended to allow user to control loading state.
We'd like to see an API like:
const [data, {pending}] = useResource('someResource', api.fetchUserList, {timeout: 1500});
But this may introduce complexity on useResourceAll
thus we may require a more complete API redesign or to remove this function (since explicit naming allows different fetch function in each call).
I don't think asking for a key
is a good idea. useSWR
which requires a key
as unique identifier is less convenient than useRequest
of huse for developers. The later, which uses asyncTask and params as cache key, is insensitive for callers but reach the same effect. Even though key
is required, how can developers get it? Especially when developers extract similar parts into hooks, it's hard to specify the key but have to append stringified-params to api url just like what happens inside useRequest and in this repo. So I think scope+task+params
is enough to generate the key automatically and key could be optional.
The essential problem is about SSR, WeakMap
s are not serializable.
With react 18's roadmap and an official <Cache />
component coming, I'm wondering if version 2.0 can ever be shipped 😞