Hello, I'm Eduardo, I'm a Vue.js core team member, I work as a freelance front-end developer and I'm the author of uRouter and Pina, among other libraries, and you can find me in person in Paris in some of the meetups or online as Pozva.
And today I want to talk to you about a topic that is very dear to me because it relates a lot to the router, which is data loaders. This is way more than just data fetching, actually, but I think data fetching is going to be the most common use case for this feature.
And currently, if you are interested or keen into learning all the technical details about this feature, you can already do so by reading the RFC. I even have a website up for it. But as you can see, this is pretty long. It's really going through every single technical detail that I could think of. There is really a lot, but it does cover more than documentation would even cover because it also covers some edge cases. Of course, this is a little bit boring to explain, so my goal today is to kind of make you skip the whole RFC by just showing you how to use it. Of course, I won't be able to go in all the details of the implementation of data loaders, but I will still show you what problem we are solving and how we are solving.
So the problem that we're solving is data fetching, which is hard. There are many ways of doing it, and I'm going to show you some ways of doing it. We have an initial data container that we're going to fill with the data that we want. We have some ref, and then we can call some API on mounted if we want a way that we're going to data and then we can display it. But then sometimes we also want to update based on the row, like an ID on the params. So then we move that to a watcher, we watch the params, and then we fetch again. Then what we might think about is, well, we still need to handle the loading state because initially the data is empty, so I might want to display some loading state. So we add that and then we have to toggle that loading state to true then to false. And then we think, oh, wait, but maybe there is an error, so I need to handle that as well. So then you have an error, which is again another ref, and you handle the error as well, and then you update the template.
Now, as you can imagine and probably experience, these get reposed very quickly. And because it's something so common in applications, you end up repeating these a lot. So you're usually abstracting to a composable, and you can even use already existing composables. So for example, you could use a view query from task stack, and so you have just four lines for what we're doing in 20 maybe, or 15 lines. And this is really nice because it also offers all the features like cache, at the cost of some, of course, heavier library, but it does move apart from the route layer. So it doesn't integrate into navigation anymore. And as a result, we have a more independent data fetching, which has its own benefits, but also some inconveniences. So data loaders are here to close the gap. And so the goals of the data loaders is to integrate with the navigation cycle, so it will block or not. You can choose whatever you want.
Comments