And the slider is considered a high-priority update, so React will prioritize that movement and then handle this bubble chart update afterwards. Use deferred value. So this is quite similar to useTransition, but instead of assigning priority to a state update, what we can do with this is basically defer the updates of a value on the UI. So if there's a value that I have, and I don't naturally need it to be updated immediately, I would then use this hook.
So it's specifically for values that might potentially affect my user experience that are not critical to the user's, to the components, or not to the components, to the user interaction, rather. So here, I just have defined the hook like normal. It takes in the value that you want to defer, and then it returns that value back to you. So here, deferred is just in a p tag, and it's considered less priority than the rest of the page.
So here's another video. It's just basically, I created this simple, just an input field, and as I type, it essentially displays what I'm typing into that input field. I've also added that red dot there, just so you can indicate when I'm actually typing. So notice again, the amount of lag there is, and just also the list that's displaying this input field has actually been artificially slowed down. Again, to kind of show how this works in practice, and then here, I'm using the hook. And you kind of notice the change. So I've defined the hook there, and the value that's returned by the hook, I'm then passing that into this slow list component. So notice again, as I'm typing, there's no lag. The red dot is there more permanently, and then the data doesn't update immediately. So again, the names don't necessarily need to be displayed immediately, but as I'm typing, which is a high priority task, I want that to happen, and I want immediate feedback.
And then the last concurrent feature today I'm going to be talking about is Suspense for Data Fetching. So Suspense has existed since React 16, but in React 18, they've essentially extended the functionality of Suspense. So typically, we would use Suspense to lazy load a component, so if we don't necessarily want the component to be rendered when it's not needed, we would use Suspense for that. And then now, what we can use Suspense for is to basically pause the render of a component until the data it requires has been resolved. So here I have an example. This data variable has to use async data, and essentially, that Suspense should have a component inside it, but the component would only be rendered after I have the data that the component is using. So here I have two examples, Data Fetching before and after. I have this ArtistList component, and this is how we typically would do it today. So we would have a useEffect where we would call this API, we would have a setIsLoading to true, and then set that to false once the result has resolved. And then I have this loadingArtist to check when loading is true, and then I'm just rendering the data. So on the right, I'm pretty much doing the same thing, but I'm using Suspense which can now handle Data Fetching. So here I have my ArtistList component wrapped inside the Suspense.
Comments