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.
Comments