{"id":6785,"date":"2025-06-15T17:32:27","date_gmt":"2025-06-15T17:32:26","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6785"},"modified":"2025-06-15T17:32:27","modified_gmt":"2025-06-15T17:32:26","slug":"build-a-todo-app-with-react-and-localstorage-3","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/build-a-todo-app-with-react-and-localstorage-3\/","title":{"rendered":"Build a Todo App with React and LocalStorage"},"content":{"rendered":"<h1>Build a Todo App with React and LocalStorage<\/h1>\n<p>Creating a Todo app is a classic project for developers to get familiar with various web technologies, especially React. In this tutorial, we\u2019ll guide you through building a simple yet powerful Todo app using React and managing application state with LocalStorage. The integration of LocalStorage will allow our app to persist tasks even when the browser is refreshed. Let\u2019s dive in!<\/p>\n<h2>What You Will Learn<\/h2>\n<ul>\n<li>How to set up a React application<\/li>\n<li>Understanding component structure in React<\/li>\n<li>Managing state with hooks<\/li>\n<li>Utilizing LocalStorage for data persistence<\/li>\n<li>Basic styling to enhance the UI<\/li>\n<\/ul>\n<h2>Prerequisites<\/h2>\n<p>This tutorial assumes that you have a basic understanding of <strong>JavaScript<\/strong>, <strong>React<\/strong>, and familiarity with <strong>HTML\/CSS<\/strong>. Make sure you have Node.js installed on your system to run the React application.<\/p>\n<h2>Setting Up the React Application<\/h2>\n<p>To kick things off, we need to set up a new React application. You can easily do this using Create React App, a command-line tool that sets up a new React project with sensible defaults.<\/p>\n<pre><code>npx create-react-app todo-app<\/code><\/pre>\n<p>Navigate into your project directory:<\/p>\n<pre><code>cd todo-app<\/code><\/pre>\n<p>Now, start the development server:<\/p>\n<pre><code>npm start<\/code><\/pre>\n<p>Your browser should automatically open and display the default React application.<\/p>\n<h2>Creating the Todo App Structure<\/h2>\n<p>Next, let\u2019s build the structure of our Todo app. We will create a few components: <strong>TodoList<\/strong>, <strong>TodoItem<\/strong>, and <strong>AddTodo<\/strong>.<\/p>\n<h3>1. TodoList Component<\/h3>\n<p>Create a new file named <strong>TodoList.js<\/strong> in the <strong>src<\/strong> directory. This component will be responsible for displaying the list of Todo items.<\/p>\n<pre><code>import React from 'react';\nimport TodoItem from '.\/TodoItem';\n\nconst TodoList = ({ todos, deleteTodo }) =&gt; {\n    return (\n        &lt;ul&gt;\n            {todos.map((todo, index) =&gt; (\n                &lt;TodoItem key={index} todo={todo} deleteTodo={deleteTodo} \/&gt;\n            ))}\n        &lt;\/ul&gt;\n    );\n};\n\nexport default TodoList;<\/code><\/pre>\n<h3>2. TodoItem Component<\/h3>\n<p>Create another file named <strong>TodoItem.js<\/strong>. This component will represent each individual Todo task.<\/p>\n<pre><code>import React from 'react';\n\nconst TodoItem = ({ todo, deleteTodo }) =&gt; {\n    return (\n        &lt;li&gt;\n            {todo}\n            &lt;button onClick={deleteTodo}&gt;Delete&lt;\/button&gt;\n        &lt;\/li&gt;\n    );\n};\n\nexport default TodoItem;<\/code><\/pre>\n<h3>3. AddTodo Component<\/h3>\n<p>Create a file named <strong>AddTodo.js<\/strong>. This component will include a form to add new Todos.<\/p>\n<pre><code>import React, { useState } from 'react';\n\nconst AddTodo = ({ addTodo }) =&gt; {\n    const [value, setValue] = useState('');\n\n    const handleSubmit = (e) =&gt; {\n        e.preventDefault();\n        if (!value) return;\n        addTodo(value);\n        setValue('');\n    };\n\n    return (\n        &lt;form onSubmit={handleSubmit}&gt;\n            &lt;input\n                type=\"text\"\n                value={value}\n                onChange={(e) =&gt; setValue(e.target.value)}\n                placeholder=\"Add new todo\"\n            \/&gt;\n            &lt;button type=\"submit\"&gt;Add Todo&lt;\/button&gt;\n        &lt;\/form&gt;\n    );\n};\n\nexport default AddTodo;<\/code><\/pre>\n<h2>Managing State with Hooks<\/h2>\n<p>Now that we have the components set up, let\u2019s manage the state of our Todo app in the <strong>App.js<\/strong> file.<\/p>\n<pre><code>import React, { useState, useEffect } from 'react';\nimport TodoList from '.\/TodoList';\nimport AddTodo from '.\/AddTodo';\n\nconst App = () =&gt; {\n    const [todos, setTodos] = useState([]);\n\n    const addTodo = (todo) =&gt; {\n        setTodos([...todos, todo]);\n    };\n\n    const deleteTodo = (index) =&gt; {\n        const newTodos = todos.filter((_, i) =&gt; i !== index);\n        setTodos(newTodos);\n    };\n\n    \/\/ Use effect to fetch todos from LocalStorage\n    useEffect(() =&gt; {\n        const storedTodos = JSON.parse(localStorage.getItem('todos'));\n        if (storedTodos) {\n            setTodos(storedTodos);\n        }\n    }, []);\n\n    \/\/ Use effect to update LocalStorage whenever the todos array changes\n    useEffect(() =&gt; {\n        localStorage.setItem('todos', JSON.stringify(todos));\n    }, [todos]);\n\n    return (\n        &lt;div&gt;\n            &lt;h1&gt;My Todo List&lt;\/h1&gt;\n            &lt;AddTodo addTodo={addTodo} \/&gt;\n            &lt;TodoList todos={todos} deleteTodo={deleteTodo} \/&gt;\n        &lt;\/div&gt;\n    );\n};\n\nexport default App;<\/code><\/pre>\n<h2>Styling the Todo App<\/h2>\n<p>Let\u2019s add some basic styles to make our Todo app look more appealing. You can create a <strong>styles.css<\/strong> file in the <strong>src<\/strong> directory and import it into your <strong>index.js<\/strong> file.<\/p>\n<pre><code>\/* styles.css *\/\nbody {\n    font-family: Arial, sans-serif;\n    background-color: #f4f4f4;\n}\n\nh1 {\n    text-align: center;\n    color: #333;\n}\n\nform {\n    display: flex;\n    justify-content: center;\n    margin-bottom: 20px;\n}\n\ninput {\n    padding: 10px;\n    border: none;\n    border-radius: 5px;\n    margin-right: 10px;\n}\n\nbutton {\n    padding: 10px;\n    border: none;\n    border-radius: 5px;\n    background-color: #5cb85c;\n    color: white;\n    cursor: pointer;\n}\n\nbutton:hover {\n    background-color: #4cae4c;\n}\n\nul {\n    list-style-type: none;\n    padding: 0;\n}\n\nli {\n    background: white;\n    margin: 5px;\n    padding: 10px;\n    border-radius: 5px;\n    display: flex;\n    justify-content: space-between;\n}<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>You\u2019ve successfully built a functional Todo app using React and LocalStorage! This simple project demonstrates core React concepts such as state management and component-based architecture while also showcasing how to persist data across sessions using LocalStorage.<\/p>\n<p>From here, consider enhancing your app by adding features like editing todos, marking them as complete, or even integrating a backend API for storage. Happy coding!<\/p>\n<h2>Further Reading<\/h2>\n<ul>\n<li><a href=\"https:\/\/reactjs.org\/docs\/getting-started.html\">React Documentation<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Window\/localStorage\">MDN Web Docs: localStorage<\/a><\/li>\n<li><a href=\"https:\/\/reactjs.org\/docs\/hooks-intro.html\">Introduction to Hooks in React<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Build a Todo App with React and LocalStorage Creating a Todo app is a classic project for developers to get familiar with various web technologies, especially React. In this tutorial, we\u2019ll guide you through building a simple yet powerful Todo app using React and managing application state with LocalStorage. The integration of LocalStorage will allow<\/p>\n","protected":false},"author":80,"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-6785","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\/6785","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\/80"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6785"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6785\/revisions"}],"predecessor-version":[{"id":6786,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6785\/revisions\/6786"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}