Chapter 87: Vue ‘data’ Option

The data option

This is the original, classic way to define local reactive state in a Vue component — the way Vue worked from day one (Vue 1 → Vue 2 → early Vue 3), and the way millions of legacy projects, tutorials, and job interview questions still use it in 2026.

Even if you personally write only <script setup> + Composition API today (which is the modern recommendation), you must understand the data() option properly because:

  • You will read/maintain old code written this way
  • Many job interviews still ask about it (especially in India)
  • Some plugins, mixins, and global component patterns still rely on it
  • Vue Devtools still shows data in this format

So let’s go through it step by step — like I’m pair-programming with you and explaining both the old world and why we mostly moved away from it.

1. What is the data option? (Very simple mental model)

data is a function that returns a plain JavaScript object.

Vue takes that object, makes every property inside it reactive, and attaches it to the component instance.

JavaScript

After Vue processes this:

  • this.count becomes reactive → changing it triggers re-render
  • this.user.age is also reactive (deep reactivity)
  • You never write this.$data.count — just this.count

Vue internally does something like:

JavaScript

2. The #1 Golden Rule – Why data must be a function

Never write data as an object — always as a function.

Wrong (very common beginner mistake):

JavaScript

→ All instances of this component share the same object → changing count in one instance changes it in all instances → chaos

Correct:

JavaScript

→ Every time Vue creates a new component instance, it calls data() → gets a fresh new object → each instance has its own independent state

3. Real, Complete Example – Counter + Todo List in Options API

vue

4. Important Rules & Gotchas (2026 Must-Know)

Rule / Gotcha Correct Behavior / Best Practice
data must be a function data() { return { … } } — never data: { … }
Returned object must be plain JS No refs, no classes, no functions — just plain data (Vue makes it reactive)
Properties become reactive automatically this.count++ → re-render; this.todos.push() → re-render
Deep reactivity Nested objects/arrays are reactive too (this.user.age++ works)
Never mutate props in data Bad: this.user = this.$props.user → breaks reactivity when parent changes prop
Can return computed values? No — computed must go in computed option
Can I access this inside data()? No — this is not available yet (use created() or mounted() for that)

5. Modern Vue 3 – Why We Almost Never Use data() Anymore

In <script setup> (the 2026 standard):

vue

→ No data() function → No this → No $data → Just normal JavaScript variables at the top level → Same reactivity, but much cleaner & type-safe with TypeScript

Vue still creates an internal $data-like structure behind the scenes — but you never see it or touch it.

Quick Summary Table – data() in 2026

Question Options API (legacy) Composition API (<script setup>) What you should do in new code
How to define local state? data() { return { count: 0 } } const count = ref(0) or reactive({}) Composition API
Access in template {{ count }} {{ count }}
Access in methods/computed this.count count.value or state.count
Still used in 2026? Yes — in legacy code, some plugins, interviews Almost never (except legacy migration) Avoid unless maintaining old code
TypeScript friendly? Poor Excellent (with ref<…>, reactive<…>) Use Composition

Final 2026 Advice from Real Projects

  • In new projects → you should never write data() — use <script setup> + ref / reactive
  • When you see data() → it means Options API (legacy style)
  • Learn to read Options API — many jobs, open-source projects, tutorials still use it
  • Never teach beginners data() as primary — start with <script setup>
  • If migrating old code → gradually convert data() → ref / reactive, methods → normal functions, etc.

Your mini homework:

  1. Create the counter + todo component above in Options API
  2. Click buttons → see reactivity work
  3. Console.log this.$data in mounted → see the object
  4. Convert it to <script setup> → compare readability & TypeScript support

Any part confusing? Want to see:

  • Full Options API vs Composition API side-by-side project?
  • How data() looks in Vue Devtools?
  • Common bugs when forgetting data() must be a function?
  • Real component written in both styles?

Just tell me — we’ll convert and compare together step by step 🚀

You may also like...

Leave a Reply

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