<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Melike Altın on Medium]]></title>
        <description><![CDATA[Stories by Melike Altın on Medium]]></description>
        <link>https://medium.com/@melikealtin?source=rss-8507ab926a92------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DuCtpbXgwLvDzmxMCJ8uLQ.jpeg</url>
            <title>Stories by Melike Altın on Medium</title>
            <link>https://medium.com/@melikealtin?source=rss-8507ab926a92------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 06:54:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@melikealtin/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Vue 3 Composition API]]></title>
            <link>https://medium.com/@melikealtin/vue-3-composition-api-040b76354ec2?source=rss-8507ab926a92------2</link>
            <guid isPermaLink="false">https://medium.com/p/040b76354ec2</guid>
            <dc:creator><![CDATA[Melike Altın]]></dc:creator>
            <pubDate>Thu, 27 Jun 2024 09:41:16 GMT</pubDate>
            <atom:updated>2024-06-27T09:41:16.860Z</atom:updated>
            <content:encoded><![CDATA[<p>Explore the differences between Vue 3’s Composition API and the Options API.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XkiCdXpaD44eOCN4D93pww.png" /></figure><p>Vue.js is a popular JavaScript framework preferred by web developers. The release of Vue 3 introduces a more powerful toolset for developers, with one of the most notable innovations being the Composition API. In this article, we will explore the Vue 3 Composition API and its differences from the Options API. However, it is important to first understand the evolution of Vue.js and the rationale behind the emergence of the Composition API.</p><p><strong>The Introduction of the Composition API in Vue.js</strong></p><p>Vue.js’s evolution has been shaped in response to the needs of developers. In Vue 2, the “Options API” was commonly used to create and manage components. While this structured approach could be effective for small projects, its limitations became apparent as the complexity of applications increased. The Options API provides a structure where data, methods, computed properties, and lifecycle hooks are defined separately within Vue components. However, it became evident that this approach struggled to manage complexity in larger projects, leading to more cumbersome code.<br>The Composition API was developed in response to the limitations of the Options API and the evolving needs of Vue.js developers. It was created to provide a more flexible, modular, and reusable way of organizing and creating logic within Vue components.</p><h3>What is the Composition API?</h3><p>According to the Vue.js documentation, the Composition API is a set of APIs that allows us to use imported functions instead of specifying options when creating Vue components. This makes components more modular and enables easier management of complex applications.</p><h4>Features of the Composition API:</h4><ol><li><a href="https://vuejs.org/api/reactivity-core.html"><strong>Reactivity API:</strong></a> Assists us in directly creating reactive states, computed states, and watchers in Vue components using functions such as ref() and reactive().</li><li><a href="https://vuejs.org/api/composition-api-lifecycle.html"><strong>Lifecycle Hooks:</strong></a> Enables us to respond to important component lifecycle events such as component creation or destruction. Hooks like onMounted() and onUnmounted() allow us to programmatically connect to the component lifecycle.</li><li><a href="https://vuejs.org/api/application.html"><strong>Dependency Injection</strong></a><strong>:</strong> Allows us to leverage Vue’s dependency injection system through functions like provide() and inject(). This feature facilitates data and functionality sharing between components, especially when using the Reactivity API.</li></ol><p>Additionally, the Composition API also has the following features:</p><ul><li><strong>Setup Function:</strong> The core function of the Composition API, invoked before the component’s lifecycle hooks.</li><li><strong>Component State:</strong> Functions and variables in the Composition API are directly exposed to the template without needing to return an object.</li><li><strong>Computed Properties and Watchers:</strong> Declare computed properties and watchers using the computed and watch functions within the setup function.</li></ul><p>The Composition API provides opportunities to break down code into smaller and more meaningful pieces, and to group them accordingly. Additionally, these pieces can be reused when needed. Let’s take an example to better understand the coding structure difference between the Options API and the Composition API.</p><p>In Options API, our code would look like this:</p><pre>&lt;script&gt;<br>export default {<br>  data() {<br>    return {<br>      totalCount: 0,<br>      userName: &quot;&quot;,<br>    };<br>  },<br>  methods: {<br>    incrementTotal() {<br>      this.totalCount++;<br>    },<br>  },<br>  computed: {<br>    isExceedingLimit() {<br>      return this.totalCount &gt; 5 ? &quot;Exceeds Limit&quot; : &quot;Within Limit&quot;;<br>    },<br>    displayUser() {<br>      return this.userName ? `User: ${this.userName}` : &quot;User Not Provided&quot;;<br>    },<br>  },<br>};<br>&lt;/script&gt;<br><br>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;div&gt;total: {{ totalCount }} {{ isExceedingLimit }}&lt;/div&gt;<br>    &lt;button @click=&quot;incrementTotal()&quot;&gt;Increase Total&lt;/button&gt;<br>    &lt;input type=&quot;text&quot; name=&quot;user&quot; v-model=&quot;userName&quot; /&gt;<br>    &lt;div&gt;{{ displayUser }}&lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><p>In the Composition API, our code would look like this:</p><pre>&lt;script setup&gt;<br>import { ref, computed } from &quot;vue&quot;;<br><br>// Total logic<br>const totalCount = ref(0);<br>const incrementTotal = () =&gt; {<br>  totalCount.value++;<br>};<br>const isExceedingLimit = computed(() =&gt; {<br>  return totalCount.value &gt; 5 ? &quot;Exceeds Limit&quot; : &quot;Within Limit&quot;;<br>});<br><br>// User logic<br>const userName = ref(&quot;&quot;);<br>const displayUser = computed(() =&gt; {<br>  return userName.value ? `User: ${userName.value}` : &quot;User Not Provided&quot;;<br>});<br>&lt;/script&gt;<br><br>&lt;template&gt;<br>  &lt;div&gt;<br>    &lt;div&gt;Total: {{ totalCount }} {{ isExceedingLimit }}&lt;/div&gt;<br>    &lt;button @click=&quot;incrementTotal&quot;&gt;Increase Total&lt;/button&gt;<br>    &lt;input type=&quot;text&quot; name=&quot;user&quot; v-model=&quot;userName&quot; /&gt;<br>    &lt;div&gt;{{ displayUser }}&lt;/div&gt;<br>  &lt;/div&gt;<br>&lt;/template&gt;</pre><h3>Structure</h3><p>The usage of the Options API in Vue 2.x is straightforward and easy to understand for beginners, but it may become challenging to manage numerous options in complex projects, leading to what is known as “option bloat.” In Vue 2.x, components are typically not grouped according to their properties, which can increase file sizes and decrease readability.</p><p>Composition API RFC, a comprehensive diagram illustrating the flow of different aspects within the same component created with both Options API and Composition API, is included. Each color represents a distinct logical concern, demonstrating how utilizing composition functions with the Composition API enhances organization within components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wmVmkzS4fTrPang7691vuw.png" /></figure><h3>Reusability</h3><p>One limitation of the Options API is its inflexibility in sharing logic between components. When you want to reuse a piece of logic across multiple components, you either have to copy and paste the code or abstract it. This can make it difficult to manage code dependencies and maintain code quality as the project grows. The Composition API addresses these issues by providing a more flexible and effective way to define component behavior. It allows developers to use a functional, reactive programming style, making logic reuse and code dependency management easier. This facilitates sharing logic between components and managing code dependencies as the project scales. Additionally, isolating reactive declarative components and easier debugging aid in testing and debugging code.</p><h3>TypeScript support</h3><p>The TypeScript support provided by the Composition API in Vue 3 offers significant advantages over the Options API. While the Options API in Vue 2.x requires additional configurations and manual declaration of TypeScript types, the Composition API seamlessly integrates TypeScript support directly into the component structure. This integration simplifies the process of writing type-safe Vue components, as developers can leverage TypeScript’s static typing features more effectively. Additionally, the Composition API’s modular structure enhances TypeScript support by enabling better type inference and more concise code. Overall, the Composition API’s TypeScript support streamlines the development process and improves code reliability compared to the Options API.</p><h3><strong>Usage</strong></h3><p>The Composition API offers developers the ability to leverage the entire capability of JavaScript for defining component behavior. This flexibility enables the utilization of advanced functionalities like async/await, Promises, and third-party libraries such as RxJS within components. Consequently, it becomes simpler to create intricate, interactive components that might pose challenges or be unfeasible to develop using the Options API.</p><h3>Learning</h3><p>The Composition API might seem like the most effective option to choose. However, there are some disadvantages to the Composition API. One of them is that the learning process can be more difficult for developers who are not familiar with functional and reactive programming. Additionally, it has a steeper learning curve compared to the Options API, which may make it a less attractive choice for beginner-level users.</p><h3>Compatibility</h3><p>The usage of Vue 3 Composition API may lead to some compatibility issues during the transition from Vue 2. Particularly, transitioning projects developed in Vue 2 to Vue 3 may require some changes. This is especially crucial for large and complex projects. However, the Vue.js community provides various resources to assist developers during this transition process. As a result, the usage of Vue 3 Composition API and the transition from Vue 2 become more manageable.</p><p><strong>We can summarize the advantages of the Composition API as follows:</strong></p><ul><li>Improved logic reuse</li><li>Greater flexibility in code organization</li><li>Enhanced type interface due to Vue 3’s TypeScript integration</li><li>Reduced production bundle size and lower overhead</li></ul><p><strong>Is the Composition API more advantageous compared to the Options API?</strong></p><p>Whether the Composition API is better than the Options API depends on the specific needs and context of your project. The Composition API offers advantages such as improved code organization, better logic reuse, and enhanced TypeScript support, which may make it preferable for certain projects, especially those involving large and complex components or where TypeScript is heavily utilized. However, the Options API continues to be a viable choice for simpler projects or for those migrating from Vue 2.x who are already familiar with its syntax. Ultimately, the best choice depends on factors such as the size and complexity of your project, your team’s familiarity with each API, and your specific development requirements.</p><ul><li><a href="https://vuejs.org/guide/introduction.html">Vue.js</a></li><li><a href="https://vueschool.io/articles/vuejs-tutorials/migrating-from-vue-2-to-vue-3-new-features/">Migrating from Vue 2 To Vue 3 - New Features - Vue School Articles</a></li><li><a href="https://auth0.com/blog/getting-started-with-vue-3-composition-api/#Options-API-vs--Composition-API">Getting Started with Vue 3: Composition API</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=040b76354ec2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>