This talk has been presented at Vue.js Live 2024, check out the latest edition of this JavaScript Conference.

Workshop level: Intermediate
This talk has been presented at Vue.js Live 2024, check out the latest edition of this JavaScript Conference.
According to Juan Andres Nunez, composables are considered the Swiss Army knife of every Vue.js developer because they help build more extensible, adaptable, controllable, and trustful Vue.js applications.
A composable is a JavaScript function that uses the Composition API to encapsulate and reuse stateful logic. It is not the same as a typical utility function and requires state and reactivity.
Learning the composable pattern helps you build more extensible, adaptable, controllable, and trustful applications. It allows you to decouple reactivity from components and use it elsewhere in your application.
No, you don't need Vue 3 to use composables. From Vue 2.7 onwards, you can start using composables.
Composables replace mixins entirely. While they overlap in functionality, composables offer a more modern and flexible approach to managing state and reactivity compared to mixins.
You should use composables when you need to share common state between different components, create controllers for features, or develop reactive UI utilities. They are versatile and can fit in any layer of your application.
Some use cases for composables include state stores, controllers for features, reactive UI utilities, and any other creative uses where reactivity and state management are required.
No, composables are not limited to the UI layer. They can be used in the domain layer, data modeling, and other parts of your application, making them omnipresent and highly versatile.
Composables improve testability by allowing you to test them in isolation without needing to mount Vue components or mock dependencies. This makes them easier to test and maintain.
Vue Use is a library of hundreds of composables with excellent documentation, covering various categories like state, browser, network, and animation. It is a valuable resource for Vue.js developers to explore and utilize composables.
Welcome to this lightning talk, the Swiss Army knife of every Vue.js developer. Composables are the Swiss Army knife of every Vue.js developer. They help you build more extensible, adaptable, controllable and trustful Vue.js applications. A composable is a JavaScript function that uses the composition API to encapsulate and reuse stateful logic.
Welcome to this lightning talk, the Swiss Army knife of every Vue.js developer. My name is Juan Andres Nunez, I'm a frontend engineer and also a content creator in my spare time at my platform, squalavue.es.
Let's go straight to the point. What is the Swiss Army knife of every Vue.js developer? This is a bold affirmation, right? Well, in my opinion, composables are. Why? For many reasons that we will explore in this presentation, but there are two why's first. The first one is why you should learn this pattern, right? Or why you should give me your attention during the seven, eight or nine minutes. And this is a fair point, I understand. So let's make a deal. This is the promise. If you give me your attention, your full attention, I promise you that this pattern will help you build more extensible, adaptable, controllable and trustful Vue.js applications.
But let's go to the what first, right? Because maybe you are not involved in this composable thing. You're not aware of what it is. Well, a composable is a JavaScript function that uses composition API. By the way, this is the official Vue.js documentation to encapsulate and reuse stateful logic. Three key points here, super important. Composition API. You have to use composition API. There is no other way around it. Stateful logic, you have to have some kind of piece of state that will be mutated, right? And you want to be notified. You want to react to that. If you don't have a state, you probably don't need composition API and you probably are not working with a composable. Maybe it's a typical utility function, but not a composable, in my opinion, of course. So this is the most bare bones implementation of a composable that I can think of. We are importing the reactivity APIs from Vue. We are exporting a function, in this case, useAlert. We are doing our thing inside. We will come later to this stateless, stateful composable and we return something. We have full power to return what we really want to return. But let's go forward. Three last key points about the what.
Composables are not the same as mixins, but they completely replace mixins. You can start using composables in Vue 2.7, so there are no excuses. Composables are useful for connecting different parts of the system, sharing common state, creating controllers, reactive UI utilities, and any other creative use. They can be used everywhere and should be applied when they fit.
The convention is to start with use, useAlert, use whatever. They are not the same as mixins. They overlap. That is true, but they overcome, they replace completely mixins. So if you are super used to mixins and start using the composable, you won't miss the mixins, believe me.
Let's be honest. You don't even need Vue 3 to start using this composable thing. From Vue 2.7, you can use composable, so there is no excuse.
So when? When should you start using this pattern? When I think on composables, I think of bridges, but this is not the typical bridge, right? This is a reactive one. What it means is that you can decouple reactivity from the components and start using elsewhere. And this is if you're super used to Vue 2 and options API, this is a mental shift that is easy to do. But in the beginning, this is somewhat strange. So if you need to connect different parts of the system, well, I can use a composable.
Some use cases, state stores. Of course, you want to share some kind of common state between different components or different parts of the system and have centralized, of course. Controllers that are more or less the same as a state store. But when I think of a controller, it is maybe the gateway to a feature. We do some polling to backend, we transform the data, maybe we serve some computer properties. This for me is a controller and it's a perfect candidate for a composable. And also reactive UI utilities. The key here is reactive. If you don't have reactivity, you don't have a composable. And of course, any other creative use of this pattern. The last slide, we will mention Vue use. That for me is the most creative usage of this composable pattern. But this is four examples. Where? Well, everywhere. This is a video from one of our main projects and we started slow, but then something clicked. And we started using it when they fit. Of course, you cannot throw a composable at whatever situation.
We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career
Comments