Replies: 3 comments 1 reply
-
This is nice way to explain it.
I'm not sure if this is an intentional detail, but it's pretty accurate as to how Suspense works internally — we even literally call these wake-up calls "pings" in the source code: Another way I like to explain it is that it's like One key difference with traditional Promise-based programming is that Promises are not exposed to the user code. You don't deal with them directly. You don't
This is very on-point and is also how we like to explain it. |
Beta Was this translation helpful? Give feedback.
-
I think this is great. One thing I've started to appreciate with Suspense that sometimes get lost when we talk about the ability to just express fallbacks in Suspense is how much this opens up the ability for React to coordinate or make decisions for data dependencies. Note: we may not do all of these examples yet, but it helps explain abilities that we have with the Suspense model. For example, say there are multiple components that suspend within a boundary. React knows there are multiple, so it can render each subtree as they're ready, but wait until all of the data is available and rendered before committing. Here, this is like React waiting for all of the pages in a boundary before rendering anything. Or another example - say there are multiple suspense boundaries in a tree and the data for each comes in around the same time (<100ms). If you paged one, committed it, then paged the other, and committed it, then you'd see a bit of jank. The ideal is that we would wait and commit both if they're ready around the same time. This is like React waiting for all the pages it gets in some small ms window before rendering. Or another - say you've already rendered content and you want to refresh it. But you want the content that's already there to stay in place and interactive while the data is refreshing. In this case, you don't want to regress to a placeholder, you want to infinitely wait and then swap the new content in. This is like when you give React the pager, it just keeps what you have until you tell it the new content is ready, and swaps that in. A final example - say you're server-side rendering and the component suspends. Then ideally React will start sending what it's rendered to the client while you're waiting for the results of what suspended. Then, when that's available, it sends that to the client. That means when you page React to tell it the results are ready, React will render it and send it to the client. So, as you can see it's much more than just allowing you to render placeholders in an API ergonomic way. If it was just that, then it wouldn't too much of an improvement over user implementations. Suspense gives you the ability to tell React what your data dependencies are so it can coordinate rendering based on the data you need. |
Beta Was this translation helpful? Give feedback.
-
This is a very helpful thread! Thank you 💕 |
Beta Was this translation helpful? Give feedback.
-
Blitz.js promoted
Suspense
since inception, and I've found the following analogy to be quite helpful for explaining what it does and why that's useful:I'd then continue to talk about how Suspense allows you to colocate data fetching with presentation, programming as if all data were already available, and realising somewhat complex fallback orchestration.
I like this analogy b/c I found that the personification is fun and easier to grasp. I'm curious, which analogies do y'all use to explain Suspense to students / coworkers?
1 the pager example can be replaced with a Promise, depending on who you're talking to
Beta Was this translation helpful? Give feedback.
All reactions