In the realm of Front-end development, “Reactivity” is a fundamental concept widely utilized, yet often not fully comprehended. In the context of Front-end frameworks, reactivity in Vue.js refers to the automatic reflection of changes in the application state onto the Document Object Model (DOM).
Reactivity is an integral part of Vue.js, seamlessly integrated into the framework. To illustrate, consider a simple example of reactivity using two-way binding (via v-model):
new Vue({
el: '#app',
data: {
message: ''
}
})
In this Vue.js example, any alteration in the data model layer is instantly mirrored in the view layer:
<!-- Vue.js Template -->
<input type="text" v-model="message">
{{ message }}
For those familiar with Vue.js, this reactivity behavior is expected. However, when attempting to replicate this in vanilla JavaScript, disparities become evident. In the absence of Vue.js, standard JavaScript fails to exhibit the same reactivity. Consider the following example mimicking the Vue.js reactivity example in vanilla JS.
To comprehend this discrepancy, let’s delve into the underlying reactivity system of Vue.js.
During the initial rendering, when a data property is accessed (referred to as “touched”), its corresponding getter function is invoked.
Subsequently, a watcher is triggered, responsible for adding the data property as a dependency and invoking the component render function.
Vue’s component render function, though not simplistic in reality, essentially returns the Virtual DOM Tree with updated data properties, subsequently displayed in the view.
The core of Vue.js reactivity lies in handling changes to data properties. When a change occurs in a data property marked as a dependency, its setter function is invoked, notifying the watcher. The watcher, in turn, invokes the component render function, updating the view with the modified data property.
Now, let’s embark on recreating Vue.js reactivity in vanilla JavaScript. To comprehend this process thoroughly, we’ll break it down into building blocks.
Begin by establishing a data model, mirroring the structure of the Vue.js example:
let data = {
message: ''
}
Introduce a global variable ‘target’ to record target functions associated with data properties:
let target = null;
Create a Dependency class to manage the storage of target functions for each data property:
class Dep {
constructor() {
this.subscribers = [];
}
depend() {
if (target && !this.subscribers.includes(target)) {
this.subscribers.push(target);
}
}
notify() {
this.subscribers.forEach((sub) => sub());
}
}
Utilize getters and setters, along with Object.defineProperty(), to track changes in data properties and trigger the corresponding target functions:
Object.keys(data).forEach((key) => {
let internalValue = data[key];
const dep = new Dep();
Object.defineProperty(data, key, {Introduce a watcher function to encapsulate code that needs to run when a data property’s value changes:
let watcher = function (func) {
target = func;
target();
target = null;
};
By assembling these building blocks, we have successfully recreated the Vue.js reactivity system in vanilla JavaScript. This comprehensive approach provides a deep understanding of reactivity and its implementation in both Vue.js and vanilla JavaScript.
We’re excited to hear all about your Brand!
We take immense satisfaction in all of our projects and will do anything we can to ensure the optimal results.
Let's Talk© 2013 - 2025 Foreignerds. All Rights Reserved