Do yourself a favor: don't make it a composable
With the rise of the composition API and the growing complexity of Vue 3 projects, I see more and more code and business
logic placed inside the composables folder, for the sake of reusability and debloating components. This is a good
thing to strive for, but I believe many developers see composables as a hammer, and every part of their code as a nail.
Vue documentation mentions a lot of those points, but for some reason many of these points slip people’s minds from time to time. Maybe it is due to how well written those docs are: people just assume the subject is very basic and they can figure it out on their own, without reading the docs first (crazy mental gymnastics, I know, but I have to make some excuse for it). I felt like writing one small page of simple rules of thumb with explanations, so you can easily spot impostors in your code or during code review.
Stateless
This one is the most egregious. Take a look at this snippet:
// DON’T DO THIS
export function useValidator() {
// this is THE worst email validator I did in my entire life
const email = (arg: string) => arg.includes('@');
const firstName = (name: string) => name.length > 3;
return {
email,
firstName,
};
}
There are a couple of issues with this code:
- Every time we call
useValidator, the JS engine creates a new set of functionsemailandfirstName. It is more efficient to move function declarations to the top level, and return references to those functions instead. - This code has no reactivity and no state whatsoever.
As it is stated in the docs, a composable is a way to encapsulate and reuse stateful logic. If your logic consists of simple functions, it is much easier to reuse them as is, without wrapping them in a composable.
export const email = (arg: string) => arg.includes('@');
export const firstName = (name: string) => name.length > 3;
Not only that, but it also makes it much clearer how your code works. Specifically, that it can be reused in any context, as it has no dependency on Vue component instance, no connection to any other composable, store, etc. So you can use it in a navigation guard, business logic, a store, another composable, or even a web worker.
Lifecycle-agnostic
If you can call your code outside of the setup context like this:
<script setup>
// DON’T DO THIS
const buttonStyle = computed(() => {
return useButtonStyles().primary;
});
</script>
It means that your code is not a composable. And should not be placed inside the composables folder.
One of the most powerful features of composables is access to the current component instance. This allows composables to use lifecycle hooks and provide/inject API. While the first one is pretty self-explanatory, the provide/inject API in composables is a true powerhouse that lets you use data from plugins like vue-router, pinia, and vue-i18n (this is also why you can’t use them outside of an injection context).
Conclusion
Here is a small checklist to understand whether your code is a composable or not:
- It exposes stateful logic like stores, utilities, etc.
- It depends on a store, router or any other plugin or composable that needs to be executed during setup.
- It uses lifecycle hooks.
If your code checks none of the boxes above, just make a simple file, and export everything you need.