{"id":32905,"date":"2024-11-01T09:12:23","date_gmt":"2024-11-01T09:12:23","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32905"},"modified":"2024-11-01T11:21:31","modified_gmt":"2024-11-01T11:21:31","slug":"react-lecture-storing-diary-data-in-web-storage","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32905\/","title":{"rendered":"React Lecture: Storing Diary Data in Web Storage"},"content":{"rendered":"<p><body><\/p>\n<p>Today&#8217;s web applications are grappling with how to efficiently manage and store user data. In this tutorial, we will explore how to store diary data in web storage using React. Web storage is a convenient and effective way to store data in the browser, allowing for more data to be stored than with Flash or cookies, and enabling easy reading and writing of data without the need for server requests.<\/p>\n<h2>1. What is Web Storage?<\/h2>\n<p>Web storage has two main types: Session Storage and Local Storage. While these two have different data storage methods, they provide the same API.<\/p>\n<ul>\n<li><strong>Local Storage:<\/strong> Allows for permanent data storage. The data does not disappear even if the user closes the browser or refreshes the page.<\/li>\n<li><strong>Session Storage:<\/strong> Data is only stored for the duration of the browser session. The data disappears when the user closes the tab or exits the browser.<\/li>\n<\/ul>\n<p>Using local storage is the most suitable option for storing diary data, as the diary entries created by the user need to be preserved.<\/p>\n<h2>2. Creating a Diary App Using React and Web Storage<\/h2>\n<p>In this section, we will create a simple diary-writing application. The application will allow users to write diaries, save them in local storage, and retrieve the saved diaries. We will manage the state using React&#8217;s `useState` and `useEffect` hooks, and synchronize the data with web storage.<\/p>\n<h3>2.1 Project Setup<\/h3>\n<pre><code>npx create-react-app diary-app<\/code><\/pre>\n<p>After creating the React app with the command above, navigate into the created directory.<\/p>\n<pre><code>cd diary-app<\/code><\/pre>\n<h3>2.2 Creating Necessary Components<\/h3>\n<p>Here is the main component structure:<\/p>\n<ul>\n<li>App.js<\/li>\n<li>DiaryForm.js<\/li>\n<li>DiaryList.js<\/li>\n<\/ul>\n<h3>2.3 DiaryForm Component<\/h3>\n<p>The DiaryForm component provides a form for users to input their diary entries.<\/p>\n<pre><code>import React, { useState, useEffect } from 'react';\n\nconst DiaryForm = ({ onSubmit }) => {\n    const [diaryText, setDiaryText] = useState('');\n\n    const handleSubmit = (e) => {\n        e.preventDefault();\n        onSubmit(diaryText);\n        setDiaryText('');\n    }\n\n    return (\n        <form onSubmit=\"{handleSubmit}\">\n            <textarea onChange=\"{(e) => setDiaryText(e.target.value)}\"\n                placeholder=\"Please write your diary...\"\n                rows=\"10\"\n                required\n            ><\/textarea>\n            <button type=\"submit\">Save<\/button>\n        <\/form>\n    );\n};\n\nexport default DiaryForm;<\/code><\/pre>\n<h3>2.4 DiaryList Component<\/h3>\n<p>The DiaryList component displays the saved diary entries.<\/p>\n<pre><code>import React from 'react';\n\nconst DiaryList = ({ diaries }) => {\n    return (\n        <div>\n            <h2>Saved Diaries<\/h2>\n            <ul>\n                {diaries.map((diary, index) => (\n                    <li key=\"{index}\">{diary}<\/li>\n                ))}\n            <\/ul>\n        <\/div>\n    );\n};\n\nexport default DiaryList;<\/code><\/pre>\n<h3>2.5 App Component<\/h3>\n<p>The App component manages the overall state of the application and interacts with web storage.<\/p>\n<pre><code>import React, { useState, useEffect } from 'react';\nimport DiaryForm from '.\/DiaryForm';\nimport DiaryList from '.\/DiaryList';\n\nconst App = () => {\n    const [diaries, setDiaries] = useState([]);\n\n    useEffect(() => {\n        const savedDiaries = localStorage.getItem('diaries');\n        if (savedDiaries) {\n            setDiaries(JSON.parse(savedDiaries));\n        }\n    }, []);\n\n    const addDiary = (diary) => {\n        const updatedDiaries = [...diaries, diary];\n        setDiaries(updatedDiaries);\n        localStorage.setItem('diaries', JSON.stringify(updatedDiaries));\n    };\n\n    return (\n        <div>\n            <h1>Diary Application<\/h1>\n            <DiaryForm onSubmit=\"{addDiary}\"><\/DiaryForm>\n            <DiaryList diaries=\"{diaries}\"><\/DiaryList>\n        <\/div>\n    );\n};\n\nexport default App;<\/code><\/pre>\n<h2>3. Code Explanation<\/h2>\n<h3>3.1 State Management<\/h3>\n<p>In the code above, we manage the state of diary data (`diaries`) using `useState`. When a user inputs a new diary entry, the `addDiary` function is called to update the state and saves it to local storage.<\/p>\n<h3>3.2 Using Web Storage<\/h3>\n<p>To save diaries in local storage, we use `localStorage.setItem` and `localStorage.getItem` methods. When the application first renders, we use `useEffect` to retrieve the saved diaries.<\/p>\n<h2>4. Running the Application<\/h2>\n<p>Once all the setup is complete, you can run the application with the command below.<\/p>\n<pre><code>npm start<\/code><\/pre>\n<p>You can check the diary application by accessing <strong>http:\/\/localhost:3000<\/strong> in the browser.<\/p>\n<h2>5. Future Improvements<\/h2>\n<p>In addition to the basic features above, the following improvements can be considered:<\/p>\n<ul>\n<li>Add diary editing and deletion features<\/li>\n<li>Display the date and time of diary entries<\/li>\n<li>Apply various UI styling<\/li>\n<li>Implement functionality to search or filter diaries<\/li>\n<\/ul>\n<h2>6. Conclusion<\/h2>\n<p>In this tutorial, we built a simple application that saves and manages diary data using web storage with React. By using web storage, we can conveniently save user data, thereby enhancing the user experience of the application. Now you can create your own diary application!<\/p>\n<h2>7. Related Resources<\/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\/Web_Storage_API\">Web Storage API Documentation<\/a><\/li>\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/JSON\">JSON Object Documentation<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s web applications are grappling with how to efficiently manage and store user data. In this tutorial, we will explore how to store diary data in web storage using React. Web storage is a convenient and effective way to store data in the browser, allowing for more data to be stored than with Flash or &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32905\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;React Lecture: Storing Diary Data in Web Storage&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[123],"tags":[],"class_list":["post-32905","post","type-post","status-publish","format-standard","hentry","category-react-basics-course"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>React Lecture: Storing Diary Data in Web Storage - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/32905\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Lecture: Storing Diary Data in Web Storage - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Today&#8217;s web applications are grappling with how to efficiently manage and store user data. In this tutorial, we will explore how to store diary data in web storage using React. Web storage is a convenient and effective way to store data in the browser, allowing for more data to be stored than with Flash or &hellip; \ub354 \ubcf4\uae30 &quot;React Lecture: Storing Diary Data in Web Storage&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32905\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:12:23+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:21:31+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32905\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32905\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"React Lecture: Storing Diary Data in Web Storage\",\"datePublished\":\"2024-11-01T09:12:23+00:00\",\"dateModified\":\"2024-11-01T11:21:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32905\/\"},\"wordCount\":508,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"React basics course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32905\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32905\/\",\"name\":\"React Lecture: Storing Diary Data in Web Storage - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:12:23+00:00\",\"dateModified\":\"2024-11-01T11:21:31+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32905\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32905\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32905\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Lecture: Storing Diary Data in Web Storage\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"React Lecture: Storing Diary Data in Web Storage - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/32905\/","og_locale":"ko_KR","og_type":"article","og_title":"React Lecture: Storing Diary Data in Web Storage - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Today&#8217;s web applications are grappling with how to efficiently manage and store user data. In this tutorial, we will explore how to store diary data in web storage using React. Web storage is a convenient and effective way to store data in the browser, allowing for more data to be stored than with Flash or &hellip; \ub354 \ubcf4\uae30 \"React Lecture: Storing Diary Data in Web Storage\"","og_url":"https:\/\/atmokpo.com\/w\/32905\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:12:23+00:00","article_modified_time":"2024-11-01T11:21:31+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32905\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32905\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"React Lecture: Storing Diary Data in Web Storage","datePublished":"2024-11-01T09:12:23+00:00","dateModified":"2024-11-01T11:21:31+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32905\/"},"wordCount":508,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["React basics course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32905\/","url":"https:\/\/atmokpo.com\/w\/32905\/","name":"React Lecture: Storing Diary Data in Web Storage - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:12:23+00:00","dateModified":"2024-11-01T11:21:31+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32905\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32905\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32905\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"React Lecture: Storing Diary Data in Web Storage"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=32905"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32905\/revisions"}],"predecessor-version":[{"id":32906,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32905\/revisions\/32906"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}