Vue.js 3.0 Composition API RFC: Rethinking Code Reuse in Vue Apps

Rethinking Vue architecture. We explore the setup function, reactive ref variables, and Options API limits.

VP
SHIVAM ITCS
·26 February 2019·10 min read·1 views

Technical Overview & Strategic Context

While Vue.js 2.x's Options API (separating logic into data, methods, and computed blocks) is simple for small components, it can make large components difficult to maintain, as related logic is scattered across different options blocks. The Vue.js 3.0 Composition API RFC, published in early 2019, addresses this by introducing a setup() function, allowing developers to group related logic by task rather than framework options.

Architectural Principle: Group related logic by task inside setup functions. Using compositional reactive variables makes code more readable and reusable in large components.

Core Concepts & Architectural Blueprint

The Composition API introduces reactive primitives: ref() (for wrapping value types) and reactive() (for wrapping objects). When these variables are modified, Vue's reactivity system automatically triggers UI updates. Grouping logic inside setup functions allows developers to extract and reuse logic as compositional functions.

Performance & Capability Comparison

API ModelLogic GroupingCode ReusabilityTypeScript Integration
Options API (Vue 2)Separated into options blocks (data, methods)Uses mixins (risk of naming conflicts)Limited type safety support
Composition API (Vue 3)Grouped by task inside setup() functionCompositional functions (useFeature)Native TypeScript support

Implementation & Code Pattern

To write components using the Composition API specifications, developers should follow these steps:

  • Implement the setup method inside component options declarations.
  • Use ref or reactive to declare reactive variables.
  • Return reactive variables and methods from setup to expose them to templates.
  • Verify target component reactivity using dev tools profiles.
htmlcode
<!-- Conceptual Vue.js 3.0 Composition API structure in 2019 -->
<template>
  <div class="counter-box">
    <p>Student count: {{ count }}</p>
    <button @click="increment">Add Student</button>
  </div>
</template>

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

export default {
  setup() {
    // Declare reactive value using ref
    const count = ref(0);
    
    const increment = () => {
      count.value++; // Mutate value reactive property
    };

    // Return properties to expose them to the template view
    return { count, increment };
  }
}
</script>

Operational Governance & Future Outlook

The Vue.js 3.0 Composition API RFC introduced a powerful model for organizing code in large components. Grouping related logic by task helps improve readability and testability in complex web applications.

VP
Vijay Paliwal
Founder, SHIVAM ITCS · 18+ years enterprise & AI engineering
MCA · Ex-HiveGPT USA · Ex-Social27 Seattle
Vue.js 3.0 Composition API RFC: Rethinking Code Reuse in Vue Apps | SHIVAM ITCS Blog | SHIVAM ITCS