-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat: suspense guard in data and error getters #2066
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
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 99b7b73:
|
|
Codesandbox demo: |
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.
I'm not sure if we're going to go with this approach for suspense, since suspense is still a ongoing discussion. And this seems to change the existing behavior of suspense?
|
Added some tests. Didn't test for
My goal with this PR was to add to the discussion with a specific implementation, which I believe to be much cleaner and more resembling of the official recommendations for Suspense (where you do a specific It does change the implementation in cases where consumers read from the Either way this would fit in with the 2.0 release as well, on the possibility that it breaks for some consumers. Worth noting though that this didn't fail any existing tests. I'm sorry if the PR was unwarranted, and I can write a summary of my proposal in the original RFC (which seemed somewhat abandoned) if desired 🙂 |
|
So even something like |
|
Suspense would still need to be explicitly enabled in the options. This PR only changes when it's thrown, if the option is enabled. |
|
Ah I didn't notice that I think I'd still prefer
In our codebase we wrap swr in custom hooks like this example: interface MyData {
readonly id: number;
}
const key = 'key';
function useData() {
return useSWR<MyData>(key);
}
function useDataWithSuspense() {
const { data, ...rest } = useSWR<MyData>(key, { suspense: true });
return { data: data!, ...rest };
}
function useDataMaybeWithSuspense(suspense = true) {
return useSWR<MyData>(key, { suspense });
}Because the A solution like Just my 2 cents. |
|
Yep, I agree having a You could technically do pattern matching with the types type SWRSuspenseHook<T> = (suspense: true) => { data: T }
type SWRNormalHook<T> = (suspense?: false) => { data?: T }
type SWRHook<T> = SWRSuspenseHook<T> | SWRNormalHook<T>but good luck doing that with the complex types of the existing SWRHook type 😄 |
Alternative to #168
Fixes #5
This PR isolates the suspense guarding into a function, which is then called in the
dataanderrorgetters returned fromuseSWR.This allows you to write your suspense
useSWRs like this to avoid waterfalls;This also allows for dependency fetching
And it would also (mostly) be backwards compatible, since most usage uses deconstruction which will automatically call the
dataand/orerrorgetters, preserving earlier functionality.I believe this is a much cleaner interface than the other ones proposed. I've yet to test this completely, so it's mostly a proposal for now, feedback needed 😄