Skip to content

What is the Composition API? Managing States in Vue.js Without Confusing Components

While developing with Vue.js, inevitably two fundamental structures appear: Composition API and Options API. You might have experienced not fully understanding what each one does at first, right? I used to think everything was fine in my own project, with solid components. But then I realized that the code was all mixed up, and it was hard to tell where what was. That’s when I understood that just ‘working’ wasn’t enough. That’s exactly where these two different approaches of Vue come into play.

The Composition API is actually a newer, more ‘modern’ approach by Vue. Think of it as combining many features, data, and methods into a ‘composition’. Like mixing ingredients in a bowl while cooking, but in a more organized way. Everything has its place, and every piece has a purpose.

The Options API, on the other hand, is an older and perhaps more familiar method. Here, everything is divided into sections: Data, Methods, Computed… Like a cupboard with shelves designated for specific purposes. It’s clear and understandable, but as the code grows, especially with components that have multiple responsibilities, switching between these sections can get a bit confusing.

So, why should we switch to or use the Composition API? Essentially, because the code becomes more readable, understandable, and reusable. Imagine creating a login form with username, password fields, a ‘login’ button, and maybe a ‘forgot password’ link. In Options API, you’d have to distribute these logics into different parts. With Composition API, you can gather all login logic in one place.

And what does this mean? You can take this ‘login’ function and reuse it in another component, like taking a Lego piece and building with it elsewhere. This reusability significantly speeds up development in my opinion.

Practically speaking, the Composition API has a special function called ‘setup’. This function runs before the component is created, and you return all reactive data, methods, and computed properties you plan to use. It’s like defining the ‘building blocks’ of your component here and exposing them outward.

Along with the Composition API, new helper functions like ‘ref’ and ‘reactive’ come into play. ‘Ref’ is used to make a single value reactive—Vue detects changes instantly. ‘Reactive’ is for more complex objects. It’s the perfect way to avoid the mixed-up code problem I experienced in my old projects.

When researching about Composition API, I found many articles through Google. Many mention it as ‘better’, ‘more flexible’. At first, it seemed complicated, but as I started trying, the logic clicked.

For an example, let’s create a counter. In Options API, it looks like:

// Options API – Simple Counter

export default { data() { return { count: 0 } }, methods: { increment() { this.count++; } } }

Clear enough, right? The ‘count’ is in data, and ‘increment’ in methods. Simple.

How about with Composition API? It’s a bit different but more ‘organized’:

// Composition API – Simple Counter

import { ref } from 'vue';

export default { setup() { const count = ref(0); function increment() { count.value++; } return { count, increment }; } }

What did we do here? We imported ‘ref’, created a reactive ‘count’, started from 0, wrote an ‘increment’ function, and in the end, returned ‘count’ and ‘increment’ for use in the template. Note how we access ‘count’ as ‘count.value’ because it’s a ref. Also, in Composition API, the use of ‘this’ is less common, which helps reduce confusion.

In summary, the Composition API offers a highly flexible structure for complex scenarios. It allows you to manage multiple logics inside a component by splitting them into small, reusable functions, and then combining them inside ‘setup’. This makes your code more modular and testable, which is especially beneficial in larger projects.

Of course, like any new technology, it has a learning curve. But once you understand the concept, you’ll see how powerful the Composition API is compared to Options API. If you want to reuse code elsewhere or work in a team, this standardized structure helps everyone understand the code more easily.

Using the Composition API integrates smoothly with Vue Router or Pinia. No worries about compatibility, as they work seamlessly together. You can find plenty of tutorials on YouTube for further learning.

In conclusion, if you develop projects with Vue.js and want your code to be more manageable, flexible, and clear, I highly recommend trying out the Composition API. Although it may seem different initially, the long-term benefits are clean, understandable code. Isn’t that great?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.