{"id":8085,"date":"2025-07-20T19:32:35","date_gmt":"2025-07-20T19:32:34","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=8085"},"modified":"2025-07-20T19:32:35","modified_gmt":"2025-07-20T19:32:34","slug":"build-a-todo-app-with-react-and-localstorage-8","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/build-a-todo-app-with-react-and-localstorage-8\/","title":{"rendered":"Build a Todo App with React and LocalStorage"},"content":{"rendered":"<h1>Build a Todo App with React and LocalStorage: A Step-by-Step Guide<\/h1>\n<p>To-do lists are a fundamental part of keeping track of tasks, projects, and schedules. They\u2019re simple yet effective tools for personal productivity. Building a Todo app provides a perfect opportunity to work with React, a powerful JavaScript library for building user interfaces, and to get acquainted with local storage for data persistence. In this guide, we will walk through creating a Todo app from scratch using React and LocalStorage.<\/p>\n<h2>Why Use React for Your Todo App?<\/h2>\n<p>React provides a component-based architecture that promotes the reuse of code and makes maintenance easier. Its virtual DOM enhances performance, especially in applications where updates are frequent, such as when managing a list of tasks.<\/p>\n<h2>Setting Up Your Development Environment<\/h2>\n<p>Before diving into code, ensure you have the necessary tools:<\/p>\n<ul>\n<li><strong>Node.js:<\/strong> Download and install from <a href=\"https:\/\/nodejs.org\/\">nodejs.org<\/a>.<\/li>\n<li><strong>Code Editor:<\/strong> Use any code editor of your choice. Visual Studio Code is highly recommended.<\/li>\n<\/ul>\n<p>Now, let\u2019s create our React application.<\/p>\n<h3>Creating a New React App<\/h3>\n<p>To set up a new React app, run the following command in your terminal:<\/p>\n<pre><code>npx create-react-app todo-app<\/code><\/pre>\n<p>Once the setup is complete, navigate into the app directory:<\/p>\n<pre><code>cd todo-app<\/code><\/pre>\n<h3>Installing Required Libraries<\/h3>\n<p>For this tutorial, we won\u2019t be using any additional libraries; React comes packed with features sufficient for our Todo app. However, if you want to manage state more efficiently in larger applications, consider installing libraries like Redux.<\/p>\n<h2>Structures of the App<\/h2>\n<p>We will divide our app into a few simple components:<\/p>\n<ul>\n<li><strong>App:<\/strong> The main component that will hold our state.<\/li>\n<li><strong>TodoList:<\/strong> This component will render the list of to-do items.<\/li>\n<li><strong>TodoItem:<\/strong> Each individual item in our to-do list.<\/li>\n<li><strong>AddTodo:<\/strong> A component that allows users to add new tasks.<\/li>\n<\/ul>\n<h2>Setting Up Our Main App Component<\/h2>\n<p>Open <code>src\/App.js<\/code> and start with the following code:<\/p>\n<pre><code>import React, { useState, useEffect } from 'react';\nimport TodoList from '.\/TodoList';\nimport AddTodo from '.\/AddTodo';\n\nfunction App() {\n    const [todos, setTodos] = useState([]);\n\n    useEffect(() =&gt; {\n        const storedTodos = JSON.parse(localStorage.getItem('todos'));\n        if (storedTodos) {\n            setTodos(storedTodos);\n        }\n    }, []);\n\n    useEffect(() =&gt; {\n        localStorage.setItem('todos', JSON.stringify(todos));\n    }, [todos]);\n\n    const addTodo = (todo) =&gt; {\n        setTodos([...todos, todo]);\n    };\n\n    const removeTodo = (todoToRemove) =&gt; {\n        setTodos(todos.filter(todo =&gt; todo !== todoToRemove));\n    };\n\n    return (\n        <div>\n            <h1>Todo App<\/h1>\n            \n            \n        <\/div>\n    );\n}\n\nexport default App;<\/code><\/pre>\n<p>Here\u2019s a brief breakdown of what\u2019s happening:<\/p>\n<ul>\n<li>We initialize the <code>todos<\/code> state as an empty array using <code>useState<\/code>.<\/li>\n<li>In the first <code>useEffect<\/code>, we retrieve any saved tasks from <code>localStorage<\/code> when the component mounts.<\/li>\n<li>In the second <code>useEffect<\/code>, we save the current state of todos to <code>localStorage<\/code> whenever it changes.<\/li>\n<li>We define the <code>addTodo<\/code> and <code>removeTodo<\/code> functions to manage the to-do items.<\/li>\n<\/ul>\n<h2>Creating the Add Todo Component<\/h2>\n<p>Next, let\u2019s create the <strong>AddTodo<\/strong> component. Create a new file in <code>src<\/code> called <code>AddTodo.js<\/code> and add the following code:<\/p>\n<pre><code>import React, { useState } from 'react';\n\nfunction AddTodo({ addTodo }) {\n    const [todo, setTodo] = useState('');\n\n    const handleAddTodo = (event) =&gt; {\n        event.preventDefault();\n        if (!todo) return;\n        addTodo(todo);\n        setTodo('');\n    };\n\n    return (\n        \n             setTodo(e.target.value)}\n                placeholder=\"Add a new task...\"\n            \/&gt;\n            <button type=\"submit\">Add<\/button>\n        \n    );\n}\n\nexport default AddTodo;<\/code><\/pre>\n<h2>Creating the Todo List Component<\/h2>\n<p>Now, let\u2019s create the <strong>TodoList<\/strong> component that renders each Todo item. Create a new file in <code>src<\/code> named <code>TodoList.js<\/code>:<\/p>\n<pre><code>import React from 'react';\nimport TodoItem from '.\/TodoItem';\n\nfunction TodoList({ todos, removeTodo }) {\n    return (\n        <ul>\n            {todos.map((todo, index) =&gt; (\n                \n            ))}\n        <\/ul>\n    );\n}\n\nexport default TodoList;<\/code><\/pre>\n<h2>Creating the Todo Item Component<\/h2>\n<p>Next, let\u2019s implement the <strong>TodoItem<\/strong> component in the <code>TodoItem.js<\/code> file:<\/p>\n<pre><code>import React from 'react';\n\nfunction TodoItem({ todo, removeTodo }) {\n    return (\n        <li>\n            {todo}\n            <button> removeTodo(todo)}&gt;Remove<\/button>\n        <\/li>\n    );\n}\n\nexport default TodoItem;<\/code><\/pre>\n<h2>Styling the Application<\/h2>\n<p>Given the basic structure of our Todo app, let\u2019s add some simple styling. Create a CSS file called <code>App.css<\/code> in the <code>src<\/code> directory:<\/p>\n<pre><code>body {\n    font-family: Arial, sans-serif;\n    background-color: #f7f7f7;\n}\n\nh1 {\n    text-align: center;\n}\n\nform {\n    display: flex;\n    justify-content: center;\n    margin: 20px 0;\n}\n\ninput[type=\"text\"] {\n    padding: 10px;\n    border: 1px solid #ccc;\n    border-radius: 5px;\n    width: 300px;\n}\n\nbutton {\n    padding: 10px;\n    margin-left: 10px;\n    background-color: #28a745;\n    color: white;\n    border: none;\n    border-radius: 5px;\n    cursor: pointer;\n}\n\nbutton:hover {\n    background-color: #218838;\n}\n\nul {\n    list-style-type: none;\n    padding: 0;\n}\n\nli {\n    display: flex;\n    justify-content: space-between;\n    padding: 10px;\n    margin: 5px;\n    background-color: white;\n    border-radius: 5px;\n    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);\n}<\/code><\/pre>\n<p>Don\u2019t forget to import this CSS file in your main <code>App.js<\/code> file:<\/p>\n<pre><code>import '.\/App.css';<\/code><\/pre>\n<h2>Running Your Application<\/h2>\n<p>Now that everything is set up, it\u2019s time to see your Todo app in action. Run the following command in your terminal:<\/p>\n<pre><code>npm start<\/code><\/pre>\n<p>This will start your React application, and you should be able to see it in your browser at <strong>http:\/\/localhost:3000<\/strong>.<\/p>\n<h2>Enhancements and Next Steps<\/h2>\n<p>Your basic Todo app is up and running! Here are some enhancements you can consider:<\/p>\n<ul>\n<li><strong>Editing Todos:<\/strong> Implement functionality to edit existing todos.<\/li>\n<li><strong>Task Completion:<\/strong> Add a feature to mark tasks as complete.<\/li>\n<li><strong>Filter Todos:<\/strong> Allow users to filter tasks by completed or pending.<\/li>\n<li><strong>Styling Improvements:<\/strong> Use CSS frameworks like Bootstrap or Tailwind CSS for improved aesthetics.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Building a Todo app with React and LocalStorage is a fantastic way to enhance your web development skills. You have learned how to handle state, manage component communication, and utilize local storage for data persistence. Continue building upon this base and exploring the rich ecosystem of React to create even more powerful applications!<\/p>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Build a Todo App with React and LocalStorage: A Step-by-Step Guide To-do lists are a fundamental part of keeping track of tasks, projects, and schedules. They\u2019re simple yet effective tools for personal productivity. Building a Todo app provides a perfect opportunity to work with React, a powerful JavaScript library for building user interfaces, and to<\/p>\n","protected":false},"author":87,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[398],"tags":[224],"class_list":["post-8085","post","type-post","status-publish","format-standard","category-react","tag-react"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=8085"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8085\/revisions"}],"predecessor-version":[{"id":8086,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/8085\/revisions\/8086"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=8085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=8085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=8085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}