What is Vue 3 Reactivity ?
Last Updated :
24 Apr, 2025
Reactivity in Vue refers to the ability of the framework to track changes to data and automatically update the corresponding parts of the user interface. When data changes, Vue efficiently determines which components need to be re-rendered, ensuring that the UI remains in sync with the underlying data.
Vue achieves reactivity through a combination of JavaScript's getter/setter mechanism, a dependency tracking system, and a virtual DOM. The syntax and approach for enabling reactivity in Vue are straightforward and intuitive. To make a data property reactive, you define it within a Vue instance's "data" option or use the "reactive" function from the Vue Composition API. Vue automatically converts each property into a reactive getter/setter during the initialization process. Here's an example of defining reactive data in Vue:
Syntax: Syntax for Vue 2.x:
new Vue({
data: {
message: "Hello, Vue!",
count: 0,
},
});
Syntax: Syntax for Vue 3.x with Composition API
import { reactive } from 'vue';
const state = reactive({
message: "Hello, Vue!",
count: 0,
});
Once a data property is reactive, you can access it in your template or component methods, and Vue will automatically track dependencies. When a reactive property is accessed during the render, Vue knows that it should be included in the dependency graph for that component.
We will understand Vue reactivity with the help of examples.
Approach 1: In this approach, we declare the counter variable using Vue.ref() and initialize it with a value of 0. The incrementCounter method increments the counter value. In the HTML section, we use Vue template syntax {{ counter }} to display the current value of the counter and @click to bind the incrementCounter method to the button's click event. Finally, we mount the Vue app to the #app element using app.mount('#app'). When you open the HTML file in the browser, you'll see the "Counter" component with the counter value displayed and an "Increment" button. Clicking the button will increment the counter, and the updated value will be shown on the page.
Example: This example illustrates the basic implementation for vue reactivity.
HTML
<template>
<div>
<p>Counter: {{ counter }}</p>
<button @click="incrementCounter">
Increment
</button>
</div>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
// Declare a reactive variable
const counter = ref(0);
// Define a method to increment the counter
const incrementCounter = () => {
counter.value++;
};
// Return the reactive variables and methods
return {
counter,
incrementCounter,
};
},
};
</script>
<style scoped>
button {
margin-top: 10px;
}
</style>
Output:
.gif)
Approach 2: In this approach, we have an input field (<input>) where users can enter a new item, and a button (<button>) to add the item to the list. The list is displayed using an unordered list (<ul>) and each item is rendered using a v-for directive. Inside the data function, we define two properties: newItem and items. The newItem represents the value entered in the input field, and items are an array that stores the list of items. The addItem method is triggered when the button is clicked. It checks if newItem is not empty, creates a new item object with a unique ID and the name from newItem, pushes it to the items array, and then resets newItem to an empty string.
The v-for directive in the template iterates over the items array and renders an <li> element for each item. The :key attribute ensures that each rendered item has a unique identifier. Whenever a new item is added (this.items.push(...)), Vue detects the change and automatically re-renders the component, updating the view to display the updated list of items.
Example: This is another example of view reactivity in Vue.js, where changes in the underlying data (the items array) automatically trigger updates in the rendered view, keeping them in sync. Here, users can add new items to the list by entering a value in an input field and clicking a button.
HTML
<template>
<div>
<input v-model="newItem"
type="text"
placeholder="Enter a new item">
<button @click="addItem">
Add
</button>
<ul>
<li v-for="item in items"
:key="item.id">
{{ item.name }}
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
newItem: '',
items: []
};
},
methods: {
addItem() {
if (this.newItem !== '') {
this.items.push({
id: Date.now(),
name: this.newItem
});
this.newItem = '';
}
}
}
};
</script>
Output:
Items are added to the list by entering the value in the input field.
Similar Reads
What is React?
React JS is a free library for making websites look and feel cool. It's like a special helper for JavaScript. People from Facebook and other communities work together to keep it awesome and up-to-date. React is Developed by Facebook, React is a powerful JavaScript library used for building user inte
6 min read
What's new in Vue 3?
Vue is a Progressive Javascript Framework for building UI and single-page applications. It is an open-source Model-View-ViewModel (MVVM) framework. The core framework is primarily focused on the view layer and it can be easily integrated with other libraries and projects. Using modern tooling and su
3 min read
What is VueJS ?
Vue.js is a free JavaScript framework for building interactive and dynamic user interfaces. It provides a special helper for JavaScript developers, similar to React. Vue.js is maintained by developers from various communities, including its creator Evan You, and is continuously updated to ensure its
5 min read
React vs Svelte: Which Is Better
What is Svelte used for? Is Svelte superior to React? Why is Svelte highly regarded? What sets Svelte apart? These questions and more are addressed in the article below.Svelte is a free, open-source front-end compiler built on JavaScript, while React is a front-end JavaScript library that relies on
10 min read
What is Redux Toolkit?
Redux Toolkit is a powerful and efficient library that simplifies managing the state in React applications using Redux. It provides a set of tools and best practices to streamline the development of complex state logic while maintaining scalability and readability. In this article, we will cover the
4 min read
Vue.js Directive
Vue.js is a progressive JavaScript framework used for building interactive user interfaces. One of its core features is directives, which allow you to extend HTML with special syntax. Directives in Vue.js provide a simple and expressive way to bind data, manage events, control rendering, and more â
6 min read
What are the features of ReactJS ?
Created by Facebook, ReactJS is a JavaScript library designed for crafting dynamic and interactive applications, elevating UI/UX for web and mobile platforms. Operating as an open-source, component-based front-end library, React is dedicated to UI design and streamlines code debugging by employing a
4 min read
Watchers in React
In React, a "watcher" refers to a mechanism that observes changes in component state or props and triggers specific actions in response. React achieves this through its component lifecycle and hooks, particularly the useEffect hook.Watchers observe changes in state or props and trigger actions.React
4 min read
What are middlewares in React Redux ?
In React Redux, middlewares are an essential concept for handling side effects and enhancing the functionality of Redux. They are used to intercept actions sent to the Redux store and modify them before they reach the reducer or after they are dispatched.Understanding ReduxBefore diving into middlew
5 min read
What's new in Next.js 13
Next.js 13 brings a host of innovative features and improvements, elevating the framework's capabilities and enhancing the development experience. From a cutting-edge build tool to advanced routing and server-side rendering features, Next.js 13 is designed to streamline workflows and boost performan
5 min read