Each component is responsible for rendering its own loading fallback, and it's going to render data as soon as its network request returns that data. We don't have any greater control over it than that, we're just letting these components render when the data is ready.
It's a bit visually distracting, having those four areas load in entirely dependent on when that network request resolves. So we're going to actually write some code together. We're going to transition this from a UseQuery-enabled app to a UseSuspenseQuery-enabled application.
So we're going to pull up this demo here, and again, like I said, this is what we will be working with today. Just to refresh, this is the exact same experience you saw in that video a second ago. That popcorn effect you see as that data loads in. And just to get oriented a little bit with this application here, here I have my layout here. You can see those colored boxes, each of those components there, the sidebar, the user menu, the route, and the playbar, which each represent one of those areas that load some data.
So we're going to start and actually convert one of these components over to UseSuspenseQuery. We're just going to start with the user menu there, which is that green box in the top right-hand corner. So to do so, here's my component, UseQuery. You see I have my GraphQL query, my UseQuery import, and a pretty familiar site here, that data and loading Booleans. When that loading Boolean is true, we return our loading state, and that's what we see.
So to convert this over to Suspense, I'm going to start by importing UseSuspenseQuery, replace the usage here, and something you're going to notice right away when we make this change is that we get this error here, property loading does not exist on type UseSuspenseQueryResult. And that's because the mechanics of Suspense, we don't actually need that loading Boolean because we're going to let Suspense handle this for us. So it makes no sense, we can actually remove that. And because of that, we no longer need our loading state. Something to also pay attention to here, with this, if I were to take a look at the type of that data property, if you're used to using this with UseQuery, you're used to seeing a union with undefined here as well. With the Suspense enable application, it actually works as if this were a synchronous code. You see our query type here is that UserMenu query type, which is really neat, enables some nice things about this. For this demo, we want to make a change. We like to take baby steps and kind of see what that change does to our application. So let's go back to our application here and refresh it and see what that change did.
Okay, so now we're seeing a blank screen for a split second, and at first paint we see the UserMenu was loaded in there. It was pretty quick, so maybe we can refresh again, Gerald. So you won't see the green highlight around the UserMenu in the top right because by the time our app first renders, the data is present for that UserMenu. So we're fetching the data for our UserMenu with our SuspenseEnabled hook, but we're not showing a loading fallback yet. For that, we're going to need a SuspenseBoundary.
Comments