0
0
Vueframework~5 mins

Why deep reactivity understanding matters in Vue

Choose your learning style9 modes available
Introduction

Understanding deep reactivity helps you make your Vue app update correctly when data changes, even inside nested objects or arrays.

When you want Vue to track changes inside objects or arrays deeply.
When you update nested properties and want the UI to reflect those changes automatically.
When you need to avoid bugs caused by Vue not detecting changes in nested data.
When working with complex data structures that change over time.
When optimizing performance by knowing how Vue tracks data.
Syntax
Vue
import { reactive } from 'vue';

const state = reactive({
  user: {
    name: 'Alice',
    age: 30
  }
});

// Changing nested property
state.user.age = 31;

reactive() makes an object deeply reactive by default.

Changes inside nested objects or arrays are tracked automatically.

Examples
Simple reactive object with a single property.
Vue
const state = reactive({ count: 0 });
state.count = 1;
Nested object property change is tracked deeply.
Vue
const state = reactive({ user: { name: 'Bob' } });
state.user.name = 'Robert';
Array changes like push are also reactive.
Vue
const state = reactive({ items: [1, 2, 3] });
state.items.push(4);
Sample Program

This Vue component shows a user's age and a button to increase it. Because state is deeply reactive, changing state.user.age updates the displayed age automatically.

Vue
<template>
  <div>
    <p>User age: {{ state.user.age }}</p>
    <button @click="increaseAge">Increase Age</button>
  </div>
</template>

<script setup>
import { reactive } from 'vue';

const state = reactive({
  user: {
    age: 25
  }
});

function increaseAge() {
  state.user.age++;
}
</script>
OutputSuccess
Important Notes

Vue's reactive() tracks nested changes automatically using Proxies. ref() provides the same deep reactivity when the ref holds an object.

Direct index or length mutations on arrays may not trigger updates; prefer using array mutation methods like push(), splice(), etc.

Use Vue DevTools to inspect reactivity and see what changes trigger updates.

Summary

Deep reactivity means Vue tracks changes inside nested objects and arrays.

This helps your UI update correctly when nested data changes.

Understanding this avoids bugs and improves app reliability.