Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?

Are you using Composition API the right way?


On this article we’re going to discover why we must always use Composition API learn how to do it correctly.

Why not the outdated Choices API manner?

Initially, despite the fact that the Choices API just isn’t deprecated, studying by means of the docs you will notice that it’s discouraged and the brand new advisable manner is Composition API and script setup.

From the docs:

  • Go along with Choices API in case you are not utilizing construct instruments, or plan to make use of Vue primarily in low-complexity situations, e.g. progressive enhancement.

  • Go along with Composition API + Single-File Parts for those who plan to construct full purposes with Vue.

For those who intend to solely use Composition API (together with the choices listed above), you’ll be able to shave a couple of kbs off your manufacturing bundle through a compile-time flag that drops Choices API associated code from Vue

Choices API does help you “assume much less” when writing element code, which is why many customers adore it. Nevertheless, in decreasing the psychological overhead, it additionally locks you into the prescribed code group sample with no escape hatch, which may make it troublesome to refactor or enhance code high quality in bigger scale initiatives. On this regard, Composition API offers higher long run scalability.

So in brief, utilizing Composition API as a substitute of Choices API provides you efficiency advantages, offers higher long run scalability and is the advisable manner for those who plan to construct full purposes.

Why Composition API is certainly higher?

Now that we understood that Composition API is best and is the advisable solution to write parts, let’s attempt to perceive why.

With Choices API we may have a code like this:

export default {
    knowledge: () => ({
        rely: 0,
        newUser: ''
        newTodo: ''

    computed: {
        filteredTodos(){ /*...*/ },
        filteredUsers(){ /*...*/ }

    strategies: {
        doubleCount(){ /*...*/ },
        addAndClearUser(){ /*...*/ },
        addAndClearTodo(){ /*...*/ }
Enter fullscreen mode

Exit fullscreen mode

This element offers with 3 logical issues: rely, consumer, and todo. At a primary look we do not see a difficulty as a result of the element may be very small but when we add extra to that element, we are going to see that the logical issues are compelled to be break up underneath totally different choices, situated in numerous components of the file.

Now that we all know the problem, let’s have a look at how we will resolve it with Composition API:

import { ref, computed } from 'vue'

// Depend
const rely = ref(0)
perform doubleCount(){ /*...*/ }

// Consumer
const newUser = ref('')
const filteredUsers = computed(() => { /*...*/ })
perform addAndClearUser(){ /*...*/ }

// Todo
const newTodo = ref('')
const filteredTodos = computed(() => { /*...*/ })
perform addAndClearTodo(){ /*...*/ }
Enter fullscreen mode

Exit fullscreen mode

Discover how the code associated to the identical logical concern can now be grouped collectively: we not want to leap between totally different choices blocks whereas engaged on a particular logical concern.

So now with Composition API we do not have to scroll up and down to seek out the code associated to a particular logical concern.

What’s much more thrilling is that we will transfer a gaggle of code into an exterior file with minimal effort, since we not have to shuffle the code round (like with Choices API) to be able to extract them. So if we had been to do this our element would appear to be this:

import { useCount, useUser, useTodo } from './composables'

const { rely, doubleCount } = useCount()
const { newUser, filteredUsers, addAndClearUser } = useUser()
const { newTodo, filteredTodos, addAndClearTodo } = useTodo()
Enter fullscreen mode

Exit fullscreen mode

Superb is not it? 😎🆒

construction logical issues?

So whereas the important thing takeaway from Composition API is that we have to group code associated to a particular logical concern collectively, I’ve skilled that the code could be exhausting to learn and perceive in comparison with Choices API.

And that’s as a result of apart from grouping by logical issues we have to set up a logical concern in a constant manner.

Personally, I set up the logical issues as if I had been to put in writing it with Choices API. So within the following order:

<script setup>
// Props (defineProps)
// Emits (defineEmits)
// Reactive variables (inc composables)
// Computed
// Strategies
// Watchers
// Lifecycle Hooks
// Expose (defineExpose)
Enter fullscreen mode

Exit fullscreen mode

Let me know within the feedback for those who use a special manner of organizing a logical concern.

Through the use of this constant manner of organizing logical issues, the code is less complicated to learn and perceive.

One other sample I’ve seen, is including a remark earlier than every logical concern to distinguish between them. Personally, I exploit this sample and it has helped me studying the code simpler so I extremely advocate it.

For instance:

<script setup>
import { ref } from 'vue'

// Depend 👈 this
const rely = ref(0)
perform doubleCount(){ /*...*/ }

// Consumer 👈 this
const newUser = ref('')
perform addAndClearUser(){ /*...*/ }
Enter fullscreen mode

Exit fullscreen mode

Lastly, I all the time choose perform declaration over const (arrow capabilities) for capabilities. That has resulted a great sample and one cause is that you just will not get Uncaught ReferenceError: Can not entry '<variable>' earlier than initialization 😉.


I hope this text helped you to grasp why Composition API is a greater solution to write parts and learn how to use it correctly. To start with you may not instantly see the advantages, however as your software grows, you’ll thank your self for utilizing it.

Composition API has unlocked a whole lot of prospects and it permits clear and environment friendly logic reuse. It is only a matter of time falling in love with it however it’s important to use it the proper manner and to this point in my expertise I’ve seen many devs utilizing it the unsuitable manner.

Thanks for studying this text and ensure to reside a like and share together with your community for those who favored it. 🎉

Add a Comment

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

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?