Vue 3 Fundamentals

Composition API

Ben Hong

Ben Hong

Vue 3 Fundamentals

Check out a free preview of the full Vue 3 Fundamentals course

The "Composition API" Lesson is part of the full, Vue 3 Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Ben discusses that composition API, when used with Vue, is vanilla JavaScript and allows custom code organization at the cost of having no default option. A brief overview of the experimental feature Suspense and a demonstration of rendering data from an API is also covered in this segment.


Transcript from the "Composition API" Lesson

>> Alright, we're here, took us a little while, but we're here at composition API. And it's time to talk about the brand new thing that is with view three. The first thing I want to caveat when it comes to the composition API is that even though it was introduced in view three, there actually are ways to use it on view two.

So that is one of the things that the team has tried to work really hard on is that,even though there are certain performance optimizations and sort of like type Script enhancements that have come with view three. As a whole, when it comes to feature parity, the team has actually worked really hard to try to keep it as basically cross compatible as humanly.

Be possible because they know, a lot of times people when they're working on enterprise apps, as much as developers would love to be on the cutting edge tool ,the reality is that most of time there's just no time to do that. And so rather than have developers be kind of feeling left out of the fact that they might want to rewrite their components using composition API.

We basically have plugins that allow you to plug in the composition API interview too. So again, while this is a view three feature, just know that you can use this in view two, you just need to look into the plugin. All right, so, composition API, let's talk about it.

So remember this lifecycle, diagram that we talked about earlier? Up here, you'll notice in blue, there's a very special lifecycle hook. That's called setup. And so setup has a very special meaning for us in view because why? Anytime you basically see the setup keyword, the lifecycle hook, you're basically ensured that you're using composition API.

I think that's one reason why the diagram here actually has it in parentheses. This is because as we can see here, prior to the setup function, there is nothing else. So this is what's interesting with the composition API is that it happens before the options API. So what does that really mean?

So we take a look at our code. I'm not hoping into the playground, so that we can just kind of code some stuff without messing up Scylla view, go into the playground. And then NPM run Dev. Boom, okay, we have a Pokedex ACC app and it's running. Alright, so let's go ahead and see what is going on here.

For whatever reason, it did not open it. The actual folder,there we go. Okay, so close this,close this, we're in playground right now. If we take a look at our app dot view, you'll notice here that we're running a fetch and we're doing all this stuff inside of created.

Now, I mentioned that the set of function actually happens before and knit. So actually if you think about asynchronous, they really should be run in the setup function, because that's its earliest point of starting sort of like the component lifecycle. So how that might look is that we, actually would have a setup function that's up here at the top.

And the reason I think it's important to organize your code this way and putting set up at the top is because everything else below does not exist. And it's useful to kind of think of it this way Is that this is what I'm doing inside of here. Now the thing that makes the composition API particularly unique when it comes to view is that everything inside of here is truly vanilla JavaScript.

So in other words there's, we can't just go like data, function, return, Pokedex like all this stuff is gone because why,composition API is about I guess some call it freedom and flexibility. One of the advantages of options API is that, it provides a very consistent way for you to add stuff.

Here's where my data goes, here's where my methods go, here's where my computer properties go. But over time, as people have scaled their applications and components, there are times where people found that they wanted to actually group things according to their concerns. And maybe find ways to organize their codes in different ways.

The composition API lets you do that. But what that means though, is that it's a free for all in that you have to organize everything yourself. So in the instance of setup, let's have a reactive named variable, in this case for the region of Pokemon that this is tracking.

So the first thing is to remember this is vanilla JavaScript so we actually have to declare the variables manually now. So, const, region name equals kanto, great. Now here's the thing though, when we actually look at this. So, let's look inside the Dev tools, and look at our app.

We'll see here that nothing exist right now, only the Pokedex is showing up. And the reason for that is because once you're inside of setup view, is not doing any magic for you, you have to actually start being very explicit about what's happening. So the thing about the composition API is that inside of here, view is not doing any magic for you.

So unlike when we're inside the data and methods where you can just call this context and it knows to like this. That Pokedex knows to go inside of data to go get the Pokedex, like, inside of here, everything is like, okay, you want the freedom here's freedom. And so what does that mean?

That means that at the end of setup, we actually have to manually return anything we want to expose. And at first, that might sound a little bit redundant, right? You're like, wait, why would I want to do that? Well, because what you'll see now inside the Dev tools is that, check it out, the region name is appearing.

And the reason for that is because when you're writing the JavaScript up here, you probably don't want everything being exposed to your component. There are times certain things might be called private, and there are things where you're running calculations that aren't relevant to the template and to the rest of the component.

And so that you can keep all up here. And what you're doing here is that you're returning it. And so when you return it now it's actually something that you can actually access. So let's say inside of this created method, now it can be like log, this dot region name.

And so we'll see now inside of here, there you go. The created lifecycle hook now has access to the data that we set up inside of setup. Didn't mean to do that, but that's how it worked out, okay. So the question here is, I want the ability to run an async function at this point, right?

And so your first instinct might be like, well, okay, this makes sense. What we're gonna do here is we're gonna create a, Const of Pokedex, and we'll just copy over this await function and then we'll just make sure that setup is async because this isn't a await. And then we'll return the Pokedex here and let's keep that actually alphabetical.

And then since we're returning it anyways, you don't need to define the data twice, so there we go. And then we actually don't even need this method anymore, right? Cause we've just refactored it. And then, we're running this inside of here. So actually now that I think about it, we could just say, we just need to delete this and then ideally we could then just say this dot Pokedex.

So we just see the region name and the Pokedex show up but you're gonna see view yelling at you in a pretty big way. And I wanted to show this error because most of the time this is not gonna be something you run into but especially when you're getting started and you see this error it's probably gonna be a bit confusing.

Because you're gonna look at this and if we read it, it says that set a function returned a promise, but no suspense boundary has been defined and that's a lot of words, right? You're like what the heck is going on? Well, so just to let what is suspense?

So we look inside of the docks. Suspense is another built in component that is experimental currently with view. And the idea behind this is that, it's trying to help us better offload performance when it comes to asynchronous option. So in the sense of like when you're loading something asynchronously, you can have a fallback.

So the way that code works as you can see here, is that you can see here, so you would drop in your suspense component. And okay, this is my dashboard that I'm loading a bunch of async stuff happening, but in case that fails, or frankly, it's not ready for me yet, I'm gonna go ahead and fall back to this template.

This syntax should look familiar to you, because earlier, we talked about slots. And so here's that V slot fallback. And this is already defined on the suspense component. And so what you'll end up seeing on the pages you'll see loading first, and whenever it's ready, view will automatically switch it over to the dashboard.

And so this is actually really useful. But the key thing here though, to understand though especially at this point in the recording, is that it is experimental. So this is why at this moment we're not gonna go into depth regarding how to use it and that kind of thing.

Keep an eye on the dock if you wanna play around with it, but know that this will probably be a pretty key part of a lot of framework approaches going forward. As a lot of frameworks are trying to figure out better ways of server-side rendering different things, and how to improve performance when it comes to loading JavaScript on a page and so forth.

Now, when it comes to our error though, the reason this is a problem at this particular point in time is because if we think about it from the tree itself, the app dot view is the root component of everything. So you can really suspend your root component in a state of nothingness, that does not work.

And so what you actually have to do is that, in the case that you really do need to do asynchronous operations in composition API. This is where you need to put it into a component, because the app dot view basically needs to be in a stable state. That's like, Okay, I know where everything is and then this component over here is going to do some stuff.

I'm gonna hang on to it and give it a backup. And then I can do other stuff over here. But we spend app dot view the whole thing, basically, because of how asynchronous operations work, it just runs the risk of corrupting it right? So this is why we don't run async setup at the root level at app View.

So to show how this works then is, we're gonna go ahead and create a component called Pokedex. And this is where we're gonna go ahead and then we'll have our script tag and we'll have our template, if it would auto complete sometime Emette is uncooperative with me. Okay, great.

So let's go ahead and blow this up into two columns, so that's easier to follow. We'll go ahead and bring over this asynchronous request right here. Actually, we can bring over the whole script blog. What am I talking about? Bring over the whole thing, which is great. And then this means that this script block is actually no longer needed on this app.

Otherwise we do need a script block because why, we need to import our Pokedex. So Pokedex from components Pokedex Dot View, and then we can register it on the components. And then we have Pokedex like that.
>> We've got a case mix-up in the--
>> We have a case mix-up?

With you, I did that again. Thank you for catching that. This is sometimes the trouble with the context switch. Okay, so inside of here, we'll have our template here for the pre-fetch. We don't even need this anymore, this button. So all this is gonna do is render that as a pre block and mean we can take this here and then we get the Pokedex like this okay.

So let's take a look and see if this worked out the way we wanted it to, okay. So, first thing first, we'll see that when we're taking a look at this right now, it's still yelling at us, but we actually are seeing something rendered unlike before. And it's because in this particular case, it's true we still need to actually suspense Component at this point.

And so like I said, we're just going to cover a high level uses of it. We're not going to dive deep into suspense as a component. Because again, if we think about it from a view perspective is that view, is saying, I don't know what to do with this yet.

So please give me some sort of fallback so that in case this thing completely crashes, we don't have this hanging component that's not going to do anything. So we bring in our suspense component. And we say, Okay, this is what I want you to render. But in case things don't work, here's the fallback where we say loading Pokedex data, just like that.

Suspense, suspense, suspense ,bom bom. Yes, I already know that, okay. So why? Let's restart the server real quick, just to make sure that's not anything weird getting botched.
>> You don't have to import suspense?
>> No import, It's kind of like the global component. It's just a global component that's registered.

Now What's weird? I forgot, yeah. Okay, so inside of here you're thinking I did the suspense right, right? I did it inside of my component because I gave it a fallback. But this approach doesn't work because if we think about it another way, it's happening at the child level.

And if we look at the error messages again, it actually says that the suspense boundary needs to be found at the parent tree. Because if you think about it another way, remember how we used to do the VF? This is the same kinda concept where you want the VF actually happening at the top for something like suspense, because it knows what to handle.

So, if I go ahead and delete this stuff again,this time we can actually say we'll have a suspense component here. That will then render out the Pokedex component when it's ready otherwise fallback to this text that's actually what's happening here. And once we do that, now everything is working as expected.

So again the API for suspense is experimental but this mental model now of having the ability to suspend your component and then have it switch out with some sort of fallback is something that is going to be more popular with frameworks, not just view. And so something that I wanted you all to keep in mind when it came to that.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now