How to Make localStorage Reactive in Vue.js ?
Last Updated :
28 Apr, 2025
In Vue, localStorage is used to store data locally in the browser. However, by default, changes made to localStorage are not reactive in Vue. This means that if the localStorage is updated outside of the Vue component, the component will not be aware of the changes and will not update accordingly. This can lead to issues when trying to keep the component in sync with the localStorage data. In this article, we will see how to make the localStorage reactive in Vue.js, along with understanding the basic way to implement this concept.
Vue provides a way to make localStorage reactive by using Vue's built-in reactive system. When we make a property reactive in Vue, any changes made to that property will trigger the re-rendering of the component. This makes it possible to keep the component in sync with the localStorage data.
Approach: To make localStorage reactive in Vue, we can use Vue's reactive function to create a reactive object that will hold the localStorage data. Then, we can use the watch method to listen to changes in the reactive object and update the localStorage accordingly. The following is the approach we can follow:
- Create a reactive object using Vue's reactive function and initialize it with the localStorage data.
- Watch the reactive object for changes using Vue's watch method.
- When the reactive object changes, update the localStorage with the new data.
Approach 1: In this approach, we have created a reactive object called "data" that holds the "message" property, which is initialized with the value from the localStorage. We are using the watch method to watch for changes in the "message" property and update the localStorage with the new value. The "message" property is also returned from the setup function so that it can be used in the template.
Example: This example illustrates the creation of the localStorage Reactive in Vue.js
HTML
<template>
<div>
<form @submit.prevent="addItem">
<input v-model="newItem"
placeholder="Enter item...">
<button>Add Item</button>
</form>
<ul>
<li v-for="(item, index) in items"
:key="index">
{{ item }}
<button @click="removeItem(index)">
Remove
</button>
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
newItem: '',
items: [],
};
},
computed: {
itemsFromLocalStorage() {
return JSON.parse(localStorage.getItem('items') || '[]');
},
},
watch: {
items(newItems) {
localStorage.setItem('items', JSON.stringify(newItems));
},
},
methods: {
addItem() {
this.items.push(this.newItem);
this.newItem = '';
},
removeItem(index) {
this.items.splice(index, 1);
},
},
created() {
this.items = this.itemsFromLocalStorage;
},
};
</script>
Output:
 Approach 2: In this approach, we've added a computed property totalItems which returns the total number of items in the items array. We also display this value in the template. When the component is mounted, we load the items array from localStorage using the mounted lifecycle hook. The addItem & removeItem methods are similar to the previous examples, adding and removing items from the items array respectively. However, we also update localStorage in these methods to persist the changes.
Example: This is another example that illustrates the creation of the localStorage Reactive in Vue.js
HTML
<template>
<div>
<form @submit.prevent="addItem">
<input v-model="newItem"
placeholder="Enter item...">
<button>
Add Item
</button>
</form>
<ul>
<li v-for="(item, index) in items"
:key="index">
{{ item }}
<button @click="removeItem(index)">
Remove
</button>
</li>
</ul>
<p>
Total Items: {{ totalItems }}
</p>
</div>
</template>
<script>
export default {
data() {
return {
newItem: '',
items: [],
};
},
computed: {
totalItems() {
return this.items.length;
},
},
mounted() {
// Load items from localStorage on component mount
this.items = JSON.parse(localStorage.getItem('items')) || [];
},
methods: {
addItem() {
this.items.push(this.newItem);
this.newItem = '';
localStorage.setItem('items', JSON.stringify(this.items));
},
removeItem(index) {
this.items.splice(index, 1);
localStorage.setItem('items', JSON.stringify(this.items));
},
},
};
</script>
Output:
Â
Similar Reads
How to replace all occurrences of a string using Vue.js filters ? In this article, we are going to learn how to replace all occurrences of a particular word with some other word using filters in VueJS. Vue is a progressive framework for building user interfaces. Filters are a functionality provided by Vue components that let you apply formatting and transformation
2 min read
How to set a click event once a page or view is loaded in vue.js? In this article, we will discuss how to set a click event once a page or view is loaded in vue.js. Just like every other JavaScript framework Vue also supports the Mounting hooks. Mounting hooks are often the most used hooks. They allow us to access or modify the DOM of your component just before or
2 min read
How to check an image is loaded or not in VueJS ? While inserting an Image in a VueJS project, it may fail to load due to the following reasons: Writing wrong image URL.Due to poor connection Approach: We are going to use an event in <img> to check whether an image is loaded or not in VueJS, the event we are going to use is: @load: The @load
3 min read
How to loop through a list of elements and display it in VueJS ? Vue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supportin
2 min read
How to Make localStorage Reactive in Vue.js ? In Vue, localStorage is used to store data locally in the browser. However, by default, changes made to localStorage are not reactive in Vue. This means that if the localStorage is updated outside of the Vue component, the component will not be aware of the changes and will not update accordingly. T
3 min read
How to implement DateTime localization in vue.js ? In this article, we will see the implementation of Date Time localization in Vue.js, along with knowing its basic implementation through the illustration.Date() Object: In Javascript, the Date object works with dates and times. Date objects are created by the new Date() constructor.Syntax:const date
8 min read
Select different values on two select tags in Vue.js In this article, we will see the selection of different values on a single select and two <select> tags in vue.js, along with understanding their basic implementation through the examples.HTML <select> tag is used to create a drop-down list. Adding a list of options inside the drop-down
6 min read
How to convert a normal string to a uppercase string using filter in VueJS ? Filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filter is a function that accepts a value and returns another value. The returned value is t
2 min read
How to dynamically add or remove items from a list in Vue.js ? Vue is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and supportin
1 min read
How to binding inline styles in Vue.js ? Vue.js is an open-source ModelâViewâViewModel front-end JavaScript framework for building user interfaces and single-page applications. Every Web application needs HTML CSS styles for presenting a better User Interface. Vue.js allows you to dynamically render styling with data model structure. Usual
4 min read