The Swiss Army Knife of Every Vue Developer

Rate this content
Bookmark
Composables (composition functions) are stateful/stateless functions that can leverage Vue's reactivity API, decoupling it from components. 
This shift in perspective opens the possibility for tackling common scenarios in a new and creative way.

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

FAQ

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.

Juan Andrés Núñez Charro
Juan Andrés Núñez Charro
9 min
25 Apr, 2024

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Composables are the Swiss Army knife of every Vue.js developer, helping build more extensible, adaptable, controllable, and trustful applications. They replace mixins and can be used everywhere, connecting different parts of the system and sharing common state. Composables are versatile, allowing for local or global use, and can be applied in both the domain layer and UI. They provide adaptability, controllability, easy testing, and eliminate the need to test the environment. Consider using Vue use for a library of composables and start building your own collection.

1. Introduction to Composables

Short description:

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.

2. Benefits and Use Cases of Composables

Short description:

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.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Everything Beyond State Management in Stores with Pinia
Vue.js London Live 2021Vue.js London Live 2021
34 min
Everything Beyond State Management in Stores with Pinia
Top Content
State management is not limited to complex applications and transitioning to a store offers significant benefits. Pinia is a centralized state management solution compatible with Vue 2 and Vue 3, providing advanced devtools support and extensibility with plugins. The core API of Pinia is similar to Vuex, but with a less verbose version of stores and powerful plugins. Pinia allows for easy state inspection, error handling, and testing. It is recommended to create one file per store for better organization and Pinia offers a more efficient performance compared to V-rex.
Welcome to Nuxt 3
Vue.js London Live 2021Vue.js London Live 2021
29 min
Welcome to Nuxt 3
Top Content
Nux3 has made significant improvements in performance, output optimization, and serverless support. Nuxt Bridge brings the Nitro engine for enhanced performance and easier transition between Nuxt 2 and Nuxt Read. Nuxt 3 supports Webpack 5, Bytes, and Vue 3. NextLab has developed brand new websites using Docus technology. Nuxt.js is recommended for building apps faster and simpler, and Nuxt 2 should be used before migrating to Nuxt 3 for stability. DOCUS is a new project that combines Nuxt with additional features like content modules and an admin panel.
One Year Into Vue 3
Vue.js London Live 2021Vue.js London Live 2021
20 min
One Year Into Vue 3
Top Content
Vue 3 has seen significant adoption and improvements in performance, bundle size, architecture, and TypeScript integration. The ecosystem around Vue 3 is catching up, with new tools and frameworks being developed. The Vue.js.org documentation is undergoing a complete overhaul. PNIA is emerging as the go-to state management solution for Vue 3. The options API and composition API are both viable options in Vue 3, with the choice depending on factors such as complexity and familiarity with TypeScript. Vue 3 continues to support CDN installation and is recommended for new projects.
Utilising Rust from Vue with WebAssembly
Vue.js London Live 2021Vue.js London Live 2021
8 min
Utilising Rust from Vue with WebAssembly
Top Content
In this Talk, the speaker demonstrates how to use Rust with WebAssembly in a Vue.js project. They explain that WebAssembly is a binary format that allows for high-performance code and less memory usage in the browser. The speaker shows how to build a Rust example using the WasmPack tool and integrate it into a Vue template. They also demonstrate how to call Rust code from a Vue component and deploy the resulting package to npm for easy sharing and consumption.
Vue: Feature Updates
Vue.js London 2023Vue.js London 2023
44 min
Vue: Feature Updates
Top Content
The Talk discusses the recent feature updates in Vue 3.3, focusing on script setup and TypeScript support. It covers improvements in defining props using imported types and complex types support. The introduction of generic components and reworked signatures for defined components provides more flexibility and better type support. Other features include automatic inference of runtime props, improved define emits and defined slots, and experimental features like reactive props destructure and define model. The Talk also mentions future plans for Vue, including stabilizing suspense and enhancing computer invalidations.
Local State and Server Cache: Finding a Balance
Vue.js London Live 2021Vue.js London Live 2021
24 min
Local State and Server Cache: Finding a Balance
Top Content
This Talk discusses handling local state in software development, particularly when dealing with asynchronous behavior and API requests. It explores the challenges of managing global state and the need for actions when handling server data. The Talk also highlights the issue of fetching data not in Vuex and the challenges of keeping data up-to-date in Vuex. It mentions alternative tools like Apollo Client and React Query for handling local state. The Talk concludes with a discussion on GitLab going public and the celebration that followed.

Workshops on related topic

Vue3: Modern Frontend App Development
Vue.js London Live 2021Vue.js London Live 2021
169 min
Vue3: Modern Frontend App Development
Top Content
Featured WorkshopFree
Mikhail Kuznetcov
Mikhail Kuznetcov
The Vue3 has been released in mid-2020. Besides many improvements and optimizations, the main feature of Vue3 brings is the Composition API – a new way to write and reuse reactive code. Let's learn more about how to use Composition API efficiently.

Besides core Vue3 features we'll explain examples of how to use popular libraries with Vue3.

Table of contents:
- Introduction to Vue3
- Composition API
- Core libraries
- Vue3 ecosystem

Prerequisites:
IDE of choice (Inellij or VSC) installed
Nodejs + NPM
Using Nitro – Building an App with the Latest Nuxt Rendering Engine
Vue.js London Live 2021Vue.js London Live 2021
117 min
Using Nitro – Building an App with the Latest Nuxt Rendering Engine
Top Content
Workshop
Daniel Roe
Daniel Roe
We'll build a Nuxt project together from scratch using Nitro, the new Nuxt rendering engine, and Nuxt Bridge. We'll explore some of the ways that you can use and deploy Nitro, whilst building a application together with some of the real-world constraints you'd face when deploying an app for your enterprise. Along the way, fire your questions at me and I'll do my best to answer them.
Monitoring 101 for React Developers
React Summit US 2023React Summit US 2023
107 min
Monitoring 101 for React Developers
Top Content
WorkshopFree
Lazar Nikolov
Sarah Guthals
2 authors
If finding errors in your frontend project is like searching for a needle in a code haystack, then Sentry error monitoring can be your metal detector. Learn the basics of error monitoring with Sentry. Whether you are running a React, Angular, Vue, or just “vanilla” JavaScript, see how Sentry can help you find the who, what, when and where behind errors in your frontend project. 
Workshop level: Intermediate
Build a powerful DataGrid in few hours with Ag Grid
React Summit US 2023React Summit US 2023
96 min
Build a powerful DataGrid in few hours with Ag Grid
Top Content
WorkshopFree
Mike Ryan
Mike Ryan
Does your React app need to efficiently display lots (and lots) of data in a grid? Do your users want to be able to search, sort, filter, and edit data? AG Grid is the best JavaScript grid in the world and is packed with features, highly performant, and extensible. In this workshop, you’ll learn how to get started with AG Grid, how we can enable sorting and filtering of data in the grid, cell rendering, and more. You will walk away from this free 3-hour workshop equipped with the knowledge for implementing AG Grid into your React application.
We all know that rolling our own grid solution is not easy, and let's be honest, is not something that we should be working on. We are focused on building a product and driving forward innovation. In this workshop, you'll see just how easy it is to get started with AG Grid.
Prerequisites: Basic React and JavaScript
Workshop level: Beginner
Hands-on with AG Grid's React Data Grid
React Summit 2022React Summit 2022
147 min
Hands-on with AG Grid's React Data Grid
WorkshopFree
Sean Landsman
Sean Landsman
Get started with AG Grid React Data Grid with a hands-on tutorial from the core team that will take you through the steps of creating your first grid, including how to configure the grid with simple properties and custom components. AG Grid community edition is completely free to use in commercial applications, so you'll learn a powerful tool that you can immediately add to your projects. You'll also discover how to load data into the grid and different ways to add custom rendering to the grid. By the end of the workshop, you will have created an AG Grid React Data Grid and customized with functional React components.- Getting started and installing AG Grid- Configuring sorting, filtering, pagination- Loading data into the grid- The grid API- Using hooks and functional components with AG Grid- Capabilities of the free community edition of AG Grid- Customizing the grid with React Components
Building GraphQL APIs on top of Ethereum with The Graph
GraphQL Galaxy 2021GraphQL Galaxy 2021
48 min
Building GraphQL APIs on top of Ethereum with The Graph
WorkshopFree
Nader Dabit
Nader Dabit
The Graph is an indexing protocol for querying networks like Ethereum, IPFS, and other blockchains. Anyone can build and publish open APIs, called subgraphs, making data easily accessible.

In this workshop you’ll learn how to build a subgraph that indexes NFT blockchain data from the Foundation smart contract. We’ll deploy the API, and learn how to perform queries to retrieve data using various types of data access patterns, implementing filters and sorting.

By the end of the workshop, you should understand how to build and deploy performant APIs to The Graph to index data from any smart contract deployed to Ethereum.