How does a component know that state has changed?
Vue "subscribes" a component to the data it uses. When that data changes, the component is notified and updates.
Let's break this down in detail, but in simple terms.
1. Vue makes state reactive
When you create a variable:
const count = ref(0)or
const user = reactive({ name: 'Tim' })Vue wraps them in a Proxy (in Vue 3), which intercepts operations:
- reading the value
- writing the value
This lets Vue know:
- who is using this value,
- when it changes.
2. A component "reacts" to the state it uses
When a component reads the value of count:
<p>{{ count }}</p>Vue remembers:
"This component depends on count."
This is called tracking dependency.
3. When the state changes, Vue receives a signal
For example:
count.value++Here's what happens:
- The Proxy "intercepts" the change.
- Vue marks: "the value was updated."
- Vue finds all effects and components that use this state.
- Vue triggers their re-render.
This is called triggering dependency.
The whole chain looks like this:
- The component reads the state → Vue remembers the dependency.
- The state changes (via
.valueor a direct assignment). - The reactive system "triggers" the change.
- Vue calls an update of the component.
- Only the changed piece of the DOM is updated.
A simple example
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>
<template>
<button @click="count++">{{ count }}</button>
</template>When we click the button:
count.value++
→ the Proxy records the change
→ Vue triggers an update
→ only the number in the button is updatedAn example with an object
<script setup>
import { reactive } from 'vue'
const user = reactive({
name: "Tim",
age: 25
})
</script>
<template>
<p>{{ user.age }}</p>
<button @click="user.age++">+</button>
</template>Vue tracks:
- that the component uses
user.age - that
user.agechanged
→ updates only this spot in the template.
Important: Vue does not re-render the whole component
Vue updates the DOM partially:
- only the nodes that use the changed state
- this is what makes Vue fast
Summary (short)
A component finds out about a state change thanks to Vue's reactive system.
Mechanism:
- The component reads data → Vue remembers the dependencies.
- The data changes → the Proxy tells Vue.
- Vue updates only the parts of the template that used the changed values.
Vue "subscribes" a component to the data it uses. When that data changes, the component is notified and updates. Let's break this down in detail, but in simple terms.
- Vue makes state reactive When you create a variable:
const count = ref(0)or
const user = reactive({ name: 'Tim' })Vue wraps them in a Proxy (in Vue 3), which intercepts operations:
- reading the value
- writing the value This lets Vue know:
- who is using this value,
- when it changes.
- A component "reacts" to the state it uses
When a component reads the value of
count:
<p>{{ count }}</p>Vue remembers:
"This component depends on count." This is called tracking dependency.
- When the state changes, Vue receives a signal For example:
count.value++Here's what happens:
- The Proxy "intercepts" the change.
- Vue marks: "the value was updated."
- Vue finds all effects and components that use this state.
- Vue triggers their re-render. This is called triggering dependency.
The whole chain looks like this:
- The component reads the state → Vue remembers the dependency.
- The state changes (via
.valueor a direct assignment). - The reactive system "triggers" the change.
- Vue calls an update of the component.
- Only the changed piece of the DOM is updated.
A simple example
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>
<template>
<button @click="count++">{{ count }}</button>
</template>When we click the button:
count.value++
→ the Proxy records the change
→ Vue triggers an update
→ only the number in the button is updatedAn example with an object
<script setup>
import { reactive } from 'vue'
const user = reactive({
name: "Tim",
age: 25
})
</script>
<template>
<p>{{ user.age }}</p>
<button @click="user.age++">+</button>
</template>Vue tracks:
- that the component uses
user.age - that
user.agechanged → updates only this spot in the template.
Important: Vue does not re-render the whole component Vue updates the DOM partially:
- only the nodes that use the changed state
- this is what makes Vue fast
Summary (short) A component finds out about a state change thanks to Vue's reactive system. Mechanism:
- The component reads data → Vue remembers the dependencies.
- The data changes → the Proxy tells Vue.
- Vue updates only the parts of the template that used the changed values.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.