Coding task · JavaScript and React course · Module 10: React Ecosystem and Future
Practice: Suspense for Data Fetching
use(promise) suspends the component while the promise is pending, and the nearest <Suspense> boundary shows its fallback in the meantime. The promise has to be stable between renders: one created during render would be new every time and the component would suspend forever, so we keep it in a cache outside the component (in Next.js the framework or a data library does it for you).
Task
Show constellation data from the observatory with Suspense and use() from React 19. ___BLANK1___: getConstellation stores in the cache the promise from fetchConstellation(name), so that every name is fetched only once. ___BLANK2___: ConstellationCard reads the data with use() from the promise returned by getConstellation(name) (do not create a new promise during render). ___BLANK3___: the fallback of the Suspense boundary is an element with a loading text, e.g. <p>Loading telescope data...</p>. After clicking another constellation you will see the fallback for a moment, and when you go back to one that was already fetched, the data appears at once.