Once we have the radius springs prepared, we just have to render it. So, for that, we replace the regular circle tag with an animated dot circle tag. And instead of calculating the radius directly, we get the radius from the radius spring. Finally, we can implement the running dots animation every time that we fetch new data.
Similarly to what we did for the radius, we will implement these animations using a use springs hook. To define the number of dots that should be rendered on the screen, we have to keep track of the data changes. And for that, we can use a custom use previous hook. Once we have a way of keeping track of the previous value, we can observe the changes in our data using a use effect that has data as a dependency. Inside that hook, we check if the count has increased before doing anything else. We then calculate the increment size by comparing the current count with the previous count. The increment will be used to define how many running dots we should render on the screen.
The animation happens quite fast though, so we can't really tell the difference once the number gets too big. It's easy to count when there's one or two or five running dots, but when it's a big increment like 100 or 200, we can't actually count them. We can take advantage of that for a quick performance gain. So since we can't tell the difference, we can clamp the increment to never be more than the maximum circles per bucket. This means that we'll never have more running dots or circle tags in the DOM than what's allowed by the maximum circles per bucket. Then we create an array of objects that we will use in this springs animation. By using the array method and passing the increment size, we can create an array of the length of our increment and fill it with an object that describes the animation.
There's a lot happening in there. So let's have a look at what's happening more closely. If you create an array by using the array method and passing it a number, it will create an array that is empty and has a length that you provided. You can then use the fill method to fill it with many sorts of things. For example, you can fill it with Nulls, you can fill it with strings, or in this specific case, we can fill it with objects, which are objects that are describing each one of our animations. So, each object in the array will be the same. A From cx, where we will store the initial position of the running dots when the animation starts playing. In this case, it will start inside of the checking out bucket. Then a To cx, where we store the final position of the running dots in the animation. In this case, the animation will finish when the dot is inside of the purchased bucket. Lastly, we have a radius for each dot. But remember that we talked about not being able to notice the difference when there's a lot of dots or not.
Comments