Reactivity in Vue.js allows components to automatically update when their data changes. Vue’s reactivity system ensures that changes to state are reflected in the UI without needing to manually manipulate the DOM.
In Vue 3, reactivity is more flexible thanks to the Composition API, which gives us the ref
and reactive
functions to create reactive state.
ref
for ReactivityThe ref
function is used to create reactive references for primitive values, such as numbers, strings, and booleans.
ref
:Import ref
from Vue:
import { ref } from 'vue';
2. import { ref } from 'vue';
Create a Reactive Reference:
To create a reactive reference, use the ref function:
const counter = ref(0); // Creating a reactive reference for a number
In this case, counter
is a reactive reference that will automatically trigger a re-render when its value changes.
Access and Modify the ref
Value:
To access the value of a ref
, use the .value
property:
console.log(counter.value); // Access the current value of the ref
counter.value++; // Modify the value
Example: Using ref
in a Component:
<template>
<div>
<p>Counter: {{ counter }}</p>
<button @click="increment">Increment</button>
</div>
</template>
<script>
import { ref } from 'vue';
export default {
setup() {
const counter = ref(0); // Reactive reference
const increment = () => {
counter.value++; // Increment the counter
};
return { counter, increment }; // Expose to template
}
};
</script>
counter
without .value
because Vue automatically unwraps the ref in the template.counter.value
to access or modify the value.reactive
for Reactivity with Objects and ArraysThe reactive
function is used to create reactive objects or arrays. Unlike ref
, which is used for primitive values, reactive
works with complex types like objects and arrays.
reactive
:Import reactive
from Vue:
import { reactive } from 'vue';
Create a Reactive Object:
To create a reactive object, pass the object to the reactive
function:
const state = reactive({
counter: 0,
message: 'Hello Vue!'
});
In this case, state
is a reactive object. Any change to its properties will trigger reactivity.
Access and Modify the Reactive Object:
You can directly access or modify the properties of a reactive
object without the need for .value
reactive
in a Component:<template>
<div>
<p>Counter: {{ state.counter }}</p>
<button @click="increment">Increment</button>
</div>
</template>
<script>
import { reactive } from 'vue';
export default {
setup() {
const state = reactive({
counter: 0
});
const increment = () => {
state.counter++; // Modify the reactive object directly
};
return { state, increment }; // Expose to template
}
};
</script>
state.counter
directly, and Vue will automatically react to changes in the state
object.state
object, like state.counter++
.ref
vs reactive
While both ref
and reactive
enable reactivity, they are used in different contexts:
Feature | ref |
reactive |
---|---|---|
Used for | Primitive values (numbers, strings, booleans) | Objects, arrays, and complex structures |
Accessing Value | ref.value |
Direct access to the property (state.prop ) |
Use Case | Best for single values (e.g., counters, booleans) | Best for handling multiple values (e.g., objects, arrays) |
ref
and reactive
Sometimes, you may need to combine ref
and reactive
to handle complex state management.
ref
and reactive
:<template>
<div>
<p>{{ state.counter }}</p>
<p>{{ message }}</p>
<button @click="increment">Increment</button>
</div>
</template>
<script>
import { ref, reactive } from 'vue';
export default {
setup() {
const state = reactive({
counter: 0
});
const message = ref('Hello Vue!');
const increment = () => {
state.counter++;
message.value = `Counter is now: ${state.counter}`;
};
return { state, message, increment };
}
};
</script>
In this example:
state
is a reactive object that holds the counter
.message
is a ref
that holds a string message.state
and message
are reactive, and changes to them will trigger the reactivity system.