How to Build a Vue To-Do App With LocalStorage

Trending 1 week ago

LocalStorage is simply a web API, built into browsers, that lets web applications shop key-value pairs connected your section device. It provides a elemental method to shop data, moreover aft you adjacent your browser.

You tin merge LocalStorage pinch your Vue apps to shop lists and different small-sized data. This allows you to support personification information crossed different app sessions.

After this tutorial, you'll person a functional Vue to-do app that tin adhd and region tasks, storing nan information utilizing LocalStorage.

Setting Up nan Vue To-Do Application

Before you statesman coding, make judge you person installed Node and NPM connected your device.

To create a caller project, tally this npm command:

npm create vue

The bid will request you to prime a preset for your caller Vue exertion earlier creating and installing nan basal dependencies.

You won’t request other features for this to-do app, truthful take "No" for each nan disposable presets.

A bid statement showing nan process of creating a caller Vue app

With nan task group up, you tin commencement building nan to-do exertion pinch LocalStorage.

Creating nan To-Do Application

For this tutorial, you will create 2 Vue components: App.vue for nan wide building and Todo.vue to show a database of tasks.

Creating nan To-Do Component

For nan Todo component, create a caller file, src/components/Todo.vue. This record will grip nan building of nan database of tasks.

Paste nan pursuing codification into nan Todo.vue file:

<script setup>
const props = defineProps(['todos']);
const emit = defineEmits(['remove-todo']);

const removeTodo = (index) => {
  emit('remove-todo', index);

  <div class="todo-list">
      <li v-for="(todo, index) successful props.todos" :key="index" class="todo-item">
        {{ todo.text }}
        <button @click="removeTodo(index)" class="remove-button">

The supra codification snippet specifications nan building of nan Todo component. It sets up nan constituent to person information and emit events done nan usage of props and civilization events, respectively.

To explicate further, nan codification employs Vue props for communicating betwixt components to person nan todos array from its genitor component, App.vue. It past uses nan v-for directive for rendering lists to iterate done nan received todos array.

The codification besides emits a civilization event, remove-todo, pinch a payload index. This allows you to region a peculiar task pinch a circumstantial scale successful nan todos array.

On clicking nan Remove button, nan snippet triggers nan emanation of nan civilization arena to nan genitor component. This indicates that you person clicked nan button, prompting nan execution of nan corresponding usability defined wrong nan genitor component, App.vue.

Creating nan Application’s View Component

Head complete to App.vue to proceed building nan Todo application. The App constituent will grip adding caller tasks and rendering nan Todo component.

Paste nan pursuing script artifact into your App.vue file:

<script setup>
import Todo from "./components/Todo.Vue";
import { ref } from "vue";

const newTodo = ref("");
const todos = ref([]);

const addTodo = () => {
  if (newTodo.value.trim() === "") return;
  todos.value.push({ text: newTodo.value });
  newTodo.value = "";

const removeTodo = (key) => {
  todos.value.splice(key, 1);

const saveToLocalStorage = () => {
  localStorage.setItem("todos", JSON.stringify(todos.value));

const savedTodos = localStorage.getItem("todos");

if (savedTodos) {

The supra codification snippet outlines nan logic of nan App.vue component. The snippet imports nan Todo constituent and initializes reactive variables pinch nan Vue Composition API.

The codification originates pinch nan import of nan Todo constituent and nan ref usability from nan specified way and vue, respectively.

The snippet past initializes a reactive string, newTodo, to shop nan task you will enter. It besides initializes an quiet reactive todos array, holding nan database of tasks.

The addTodo usability adds caller tasks to nan todos array. If newTodo is not empty, it's pushed into nan array connected confirmation and resets nan newTodo worth to let you to adhd much tasks.

The addTodo usability besides invokes nan saveToLocalStorage, which saves nan todos array into nan browser's LocalStorage. The snippet uses nan setItem method to execute this and converts nan todos array to a JSON drawstring earlier storage.

It besides defines a removeTodo usability that takes a key arsenic a parameter. It uses this cardinal to region nan corresponding todo from nan todos array. After removal, nan removeTodo usability calls nan saveToLocalStorage to update nan LocalStorage data.

Finally, nan codification uses nan getItem method disposable to LocalStorage to fetch antecedently saved tasks pinch nan todos key. If you person saved tasks successful nan browser’s LocalStorage, nan codification pushes them into nan todos array.

Now you person handled nan logic of nan App.vue component, paste nan pursuing codification successful nan template artifact of your Vue app to create nan personification interface:

  <div class="app">
    <h1>To do List</h1>
    <div class="add-todo">
      <input v-model="newTodo" @keyup.enter="addTodo"
        placeholder="Add a caller task" class="todo-input" />

      <button @click="addTodo" class="add-button">Add</button>
    <Todo :todos="todos" @remove-todo="removeTodo" />

The template uses v-model, a method of binding information successful Vue to hindrance nan inputted todo to nan newTodo reactive string. The template besides uses nan v-on directive for handling events successful Vue done its shorthand (@).

It uses v-on to perceive to some nan click and enter cardinal events to corroborate nan newTodo.

Finally, nan template uses nan Todo constituent you created first to render nan database of todos. The :todos syntax passes nan todos array arsenic a prop to nan Todo component.

The @remove-todo syntax sets up an arena listener to seizure nan civilization arena nan Todo constituent emitted and telephone nan removeTodo usability successful response.

Now you person completed nan application, you tin take to style it to your taste. You tin preview your exertion by moving this command:

npm tally dev

You should spot a surface for illustration this:

A basal Todo exertion pinch an input container to adhd a caller task and a database of 5 existing tasks

You tin adhd aliases region tasks wrong nan Todo application. What's more, acknowledgment to nan integration of LocalStorage, you tin refresh nan exertion aliases exit it entirely; your curated to-do database will persist intact.

Importance of LocalStorage

LocalStorage integration successful web applications is basal to some novice and knowledgeable developers. LocalStorage introduces beginners to information persistence by allowing them to shop and retrieve user-generated content.

LocalStorage is important arsenic you tin guarantee your personification information remains intact crossed different sessions. LocalStorage eliminates nan request for changeless server communication, starring to faster load times and improved responsiveness successful web applications.

Source Tutorials