Current section: Windowing 33 exercises
solution

Virtualizer

Loading solution

Transcript

00:00 Let's virtualize our list, so I'm going to create a ref our we're going to call this the parent ref and This is a UI UL list element. Let's pull in use ref from react and Then down here. Let's attach that ref to the UL right here

00:18 So ref is that parent ref and with that now? We are Again, and yeah, we'll want to initialize that to know there we go, and now we're going to create our row virtualizer And that is going to be using use virtualizer use bird use

00:38 Virtualizer from ten stack react virtual which is not auto importing so let's do that ourselves From and the thing we want use virtualizer there we go Okay, so we've got our virtualizer from ten stack react to virtual We want to set a couple of options here so the count to the number of items

01:00 We're going to have is going to be how many cities there are the get scroll elements so the element that's going to be doing the scrolling is that current parent ref element and then each Element is 20 pixels tall. That's just the way we've established this

01:16 We could use some of the arguments or the or the index of that's passed to us from the estimate size function and Access the city based on that index and maybe how long the name is or whatever Information we have we can give a better estimate, but again. This is just an estimate It's not actually what the size will be

01:37 So that is useful for dynamic lists, but for us. They're all just 20 pixels All right, and then we'll come back to this here in just a moment We'll come down here, and we want to have this you will have the height of the entire content So we're going to add an Li here that has a style

01:57 Set to the total size of pixels on that row virtualizer so that effectively is just going to say Let's go through how many there are Which we Specified right here, and let's get the estimate size for all of them, and that's going to be our total size so that's however many there are times 20 for us and

02:19 now we're going to say row virtualizer get items in place of our cities and this is going to Give us a virtual item And we don't need to worry about the index here because the virtual item is going to have our index So let's get our item

02:37 Is from burr yes, or yes cities virtual item index there you go, and if there's no item here That's unlikely to happen, but we do want to handle that case We'll just not render anything if there's no item and then we're going to pluck off the index key size and start from the virtual item and

02:56 This city here, let's rename this from item to city there we go and Then a couple of things we're going to use the key from the virtual item We're going to use the index from the virtual item The size and start need to be passed down to our list item so that those can be used for the styling stuff so we'll add a size and

03:18 start and Then right down here. Let's accept the size and start props and start and then size and start and And then down here we can add these CSS rules to Position absolutely and then set the height to be equal to the size

03:41 Is a site well yeah, you could hard-code it since we're hard-coding our stuff, but we're going to do Size here and again that may not always be something you want to do it kind of depends if you've got a dynamic Virtual list maybe you just want it to size itself however it needs to be But that's going to handle that and then we're going to do a transform

04:02 Transform come on a I assistant you can help me with this. Thank you to translate Why with the start being set to the start pixels and so react virtual? There were the virtualizer is going to give that to us and all of that Should get us in a pretty good spot for this so we're on a 6x slowdown still

04:22 I can of course scroll through this and it looks kind of interesting. I'm going to click on this. We're going to hover over and remove and then stop and we'll see that things are way way faster if I do this on our profiler and I hover over one and I stop Then we'll see we are only Rendering one of our list items again

04:44 And the city chooser is rendering really fast because it only is rendering like 20 years something of those Which is awesome now you might notice as you're Running the profiler, and you've got your 6x slowdown You might notice that scrolling you can kind of see where some of the elements are not rendered quite yet that like

05:04 They'll sometimes Appear or it will be invisible and then they'll appear and that's because we're on like we're simulating a really low-end device and if that's a real concern for you, then there are options that you can give to the virtualizer to set the over scan and

05:19 Like how many additional elements pass to the scrollable view we render so if that is a problem for you, then you could configure that but we That's typically not going to be a problem the defaults are actually pretty good most of the time Okay, great. So the other problem that we've got here is how we manage scrolling so

05:39 Downshift to which we're using for the combo box this use combo box is coming from downshift. It has this Arrow key control, so if I scroll up here to the top and we'll be able to see what's going on here Inside of the the input if I go down and up Then it's going to move me to that item

06:00 But once I get past the items that have already been or that are rendered then Like the thing just totally disappears And if I go up to the top and try to scroll back to the bottom because that that's the thing It won't that won't work at all. So the thing that needs to happen is when the

06:21 When we want to scroll something into view We want to actually change down shifts behavior because now react virtual is Responsible for scrolling things into view and then when the highlight index changes we need to tell react virtual Hey, you need to scroll to this particular item. So that's what these two pieces are for. I

06:40 Gave them to you because you're not here to learn down shift But this is definitely something that if you are working with something like this you'll have to think about like oh if I have keyboard controls that controls where the Highlights are coming from then I'm going to need to manage that and that's exactly what we're doing here So we're just telling down shift. Hey, you don't need to worry about scrolling to view

07:01 I'm overriding your built-in behavior on how you scroll things into view I'm not going to do anything like that and Whenever the highlighted index changes Then we'll have row virtualizer be the one that scrolls to that index And so by setting this up now, I can this will totally work and if I

07:22 Scroll down here We can start from right here. I can Also do the over like forward and backwards So if I'm at the very top and I hit up it'll take me down to the bottom All of that is working exactly as it should Exactly as you want it to and that's thanks to react virtualizer from 10 stack

07:43 Super super cool Windowing is really impressive stuff. Definitely like take a look at how all of this ends up turning out as you're scrolling around I just think that's it's actually pretty cool. So With that we have a really really fast a scrollable list where we're reading stuff from

08:04 The web worker and we've got our virtualizer going on here We've memoized things and now we can go to Salt Lake City and have a good time Okay, good job