Vue 3 Composition API data() Function

With the Composition API, Vue 3 no longer uses data(). Use ref() or reactive() in setup() to manage your component state.

|

In Vue 2, the data() option in the Options API returns an object with your component’s state, like a simple count value.

export default {  data() {

    return {

      count: 0

    }

  }

}

Reactive Data in the Vue 3 Composition API

In Vue 3’s Composition API, there’s no data() function. Instead, you define reactive state inside the setup() function using ref() or reactive().

Example: ref()

<script setup>import { ref } from ‘vue’

 

const count = ref(0)

</script>

 

<template>

  <button @click=”count++”>Clicked {{ count }} times</button>

</template>

Example: reactive()

<script setup>import { reactive } from ‘vue’

 

const state = reactive({

  count: 0,

  name: ‘Vue 3’

})

</script>

 

<template>

  <button @click=”state.count++”>

    Clicked {{ state.count }} times

  </button>

  <p>{{ state.name }}</p>

</template>

Also Read: Best Vue Component Libraries to Build Stunning Frontends

Key Takeaway

  • In the Vue 3 Composition API, there is no data() function.
  • Use ref() for primitive values, and for any value you might fully replace later (primitive or complex).
  • Use reactive() for objects or arrays when you mostly update their properties directly.

Related Resources

Related
Vue

In Vue 2, you need to explicitly install plugins using Vue.use() to add to Vue’s capabilities. This method installs the plugin globally so all components…

10 Feb 2026
Vue

In Vue 3, the best way to define global variables is by adding them to app.config.globalProperties during app setup. This makes them accessible across the…

21 Jan 2026
Vue

In Vue 3's Composition API, watching for prop changes is essential when you need to respond to updated values passed from a parent component. Unlike…

20 Jan 2026