Avoiding waterfalls with Suspense
See original GitHub issueSide note: I am writing this with my jaw on the floor. What a clean API. Nice work guys. 😍
This issue refers to the idea of waterfalls as mentioned in the react docs. https://reactjs.org/docs/concurrent-mode-suspense.html#for-library-authors
From a glance, it appears that the suspense based API does not take the above optimization into consideration. If I am not mistaken, to avoid waterfalls, we need to declare the API calls first(which will init the fetch calls), and then read the data from another call (read
method in relay), which will throw the promise.
Is this already done in the library? If not, do you guys plan to do it in the future? How would the API look like?
PS: In my head, the API should look something like below
function MyComponent(props) {
// First we declare the list of fetch calls. Here we use an object. Could be an array instead.
// Doing this will NOT trigger suspense. i.e. A promise shall not be thrown.
// But the API calls will be triggered in parallel
const fetcher = useSuspenseSwr({
key1: {/*fetch config for key 1*/},
key2: {/*fetch config for key 2*/},
});
// and then call fetcher.fetch('key1') somewhere in the code
// This is when the promise will be thrown
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Avoiding waterfalls with Suspense · Issue #5 · vercel/swr
I'm working on my own project to simply avoid the waterfalls in this way. If it helps. // Component.tsx import { useSWR }...
Read more >Suspense for Data Fetching (Experimental) - React
Libraries can prevent waterfalls by offering a more centralized way to do data fetching. For example, Relay solves this problem by moving the...
Read more >React Suspense makes network waterfall when a component ...
I know that Suspense catches a Promise and shows Loading fallback until it is settled. So I understand why it make waterfall. Then,...
Read more >Suspense for Data Fetching (Experimental) - chan.dev
Over time, Suspense will be React's primary way to read asynchronous data ... Prefer APIs that favor or enforce fetching before render —...
Read more >React Suspense in Practice | CSS-Tricks
Has your brain turned to mush reading other things on Suspense that talked about waterfalls, fetch-on-render, preloading, etc? Don't worry about ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m working on my own project to simply avoid the waterfalls in this way. If it helps.
Here’s my solve: https://medium.com/@tamis.mike/useswr-parallel-suspense-b41929a01098