Skip to the content.

Mutation in Vuejs Composition API

Mutable refers to the ability to directly modify the state of a reactive object, while immutable refers to creating a new object when changes are needed.

In Vue.js, when you follow an immutable approach, you create a new state or object when updating the state, triggering the reactivity system to recognize the change and update the UI accordingly. This helps in better tracking and managing state changes within the framework.

It’s important to note that Vue.js provides the reactivity system to automatically detect changes in the state, and following an immutable pattern ensures that these changes are detected correctly. However, mutable operations can sometimes lead to unexpected behavior or issues with reactivity. Using the Composition API along with the principles of immutability helps in writing more predictable and maintainable Vue.js applications.

Some examples listed below are try to define what is mutable and what is immutable.

// IMMUTABLE
const message1 = ref("HELLO FROM MESSAGE 1");
const message2 = ref("HELLO FROM MESSAGE 2");
console.log(message1.value); // HELLO FROM MESSAGE 1
console.log(message2.value); // HELLO FROM MESSAGE 2
message1.value = message2.value;
console.log(message1.value); // HELLO FROM MESSAGE 2
console.log(message2.value); // HELLO FROM MESSAGE 2
message2.value = "HELLO FROM MESSAGE 3";
console.log(message1.value); // HELLO FROM MESSAGE 2
console.log(message2.value); // HELLO FROM MESSAGE 3
// IMMUTABLE
let message3 = "HELLO FROM MESSAGE 1";
let message4 = "HELLO FROM MESSAGE 2";
console.log(message3); // HELLO FROM MESSAGE 1
console.log(message4); // HELLO FROM MESSAGE 2
message3 = message4;
console.log(message3); // HELLO FROM MESSAGE 2
console.log(message4); // HELLO FROM MESSAGE 2
message4 = "HELLO FROM MESSAGE 3";
console.log(message3); // HELLO FROM MESSAGE 2
console.log(message4); // HELLO FROM MESSAGE 3
// MUTABLE
let message5 = reactive({ text: "HELLO FROM MESSAGE 1" });
let message6 = reactive({ text: "HELLO FROM MESSAGE 2" });
console.log(message5); // HELLO FROM MESSAGE 1
console.log(message6); // HELLO FROM MESSAGE 2
message5 = message6;
console.log(message5); // HELLO FROM MESSAGE 2
console.log(message6); // HELLO FROM MESSAGE 2
message6.text = "HELLO FROM MESSAGE 3";
console.log(message5); // HELLO FROM MESSAGE 3
console.log(message6); // HELLO FROM MESSAGE 3
message5.text = "HELLO FROM MESSAGE 4";
console.log(message5); // HELLO FROM MESSAGE 4
console.log(message6); // HELLO FROM MESSAGE 4
// IMMUTABLE
let message7 = reactive({ text: "HELLO FROM MESSAGE 1" });
let message8 = reactive({ text: "HELLO FROM MESSAGE 2" });
console.log(message7); // HELLO FROM MESSAGE 1
console.log(message8); // HELLO FROM MESSAGE 2
message7.text = message8.text;
console.log(message7); // HELLO FROM MESSAGE 2
console.log(message8); // HELLO FROM MESSAGE 2
message8.text = "HELLO FROM MESSAGE 3";
console.log(message7); // HELLO FROM MESSAGE 2
console.log(message8); // HELLO FROM MESSAGE 3
message7.text = "HELLO FROM MESSAGE 4";
console.log(message7); // HELLO FROM MESSAGE 4
console.log(message8); // HELLO FROM MESSAGE 3