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.

Summarize with AI

Not enough time? get the key points instantly.

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()

 

  

Example: reactive()

 

  

    Clicked {{ state.count }} times

  

  

{{ state.name }}

Also Read:

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


Resources

Insights from our engineering leaders.

Our team shares practical insights on the technologies and engineering decisions shaping enterprise software today, grounded in real client work.