{"id":32921,"date":"2024-11-01T09:12:29","date_gmt":"2024-11-01T09:12:29","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32921"},"modified":"2024-11-01T11:21:28","modified_gmt":"2024-11-01T11:21:28","slug":"react-course-diary-app-example-and-dynamic-route-routing-with-react-router","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32921\/","title":{"rendered":"React Course: Diary App Example and Dynamic Route Routing with React Router"},"content":{"rendered":"<p><body><\/p>\n<p>Hello! In this course, we will create a basic diary app using React. This app will allow users to write, edit, and delete diary entries. Additionally, we will use React Router to add dynamic path routing, so that each diary entry can be accessed individually. Through this article, I will help you gain a deep understanding of the basic concepts of React, state management, and routing.<\/p>\n<h2>1. What is React?<\/h2>\n<p>React is a UI library developed by Facebook that provides a component-based approach to building user interfaces. Using React, you can manage the state of the UI and automatically update it based on changes in data. Moreover, React allows you to create reusable components, enabling efficient organization of your application.<\/p>\n<h2>2. Project Setup<\/h2>\n<p>To create the diary app, we need to set up a React project. Use the command below to create a new React project.<\/p>\n<pre><code>npx create-react-app diary-app<\/code><\/pre>\n<p>This command will create a new React application named `diary-app`. Once the project is created, navigate to the project directory.<\/p>\n<pre><code>cd diary-app<\/code><\/pre>\n<h2>3. Installing Required Libraries<\/h2>\n<p>To use React Router, you need to install the `react-router-dom` library. Please install it using the command below.<\/p>\n<pre><code>npm install react-router-dom<\/code><\/pre>\n<h2>4. Project Structure<\/h2>\n<p>The basic folder structure of the diary app will be as follows:<\/p>\n<ul>\n<li>src\/\n<ul>\n<li>components\/ &#8211; Stores React components.<\/li>\n<li>pages\/ &#8211; Defines individual pages.<\/li>\n<li>App.js &#8211; The main application file.<\/li>\n<li>index.js &#8211; The entry point.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h2>5. Creating React Components<\/h2>\n<p>To build the basic UI structure of the diary app, we need to create several React components. For example, we will create and use the `Header`, `DiaryList`, `DiaryDetail`, and `DiaryForm` components.<\/p>\n<h3>5.1 Header Component<\/h3>\n<pre><code>import React from 'react';\n\nconst Header = () => {\n    return (\n        <header>\n            <h1>Diary<\/h1>\n        <\/header>\n    );\n};\n\nexport default Header;<\/code><\/pre>\n<h3>5.2 DiaryList Component<\/h3>\n<p>This component is responsible for displaying the list of diary entries.<\/p>\n<pre><code>import React from 'react';\nimport { Link } from 'react-router-dom';\n\nconst DiaryList = ({ diaries }) => {\n    return (\n        <div>\n            <h2>Diary List<\/h2>\n            <ul>\n                {diaries.map(diary => (\n                    <li key=\"{diary.id}\">\n                        <link to=\"{`\/diary\/${diary.id}`}\"\/>{diary.title}\n                    <\/li>\n                ))}\n            <\/ul>\n        <\/div>\n    );\n};\n\nexport default DiaryList;<\/code><\/pre>\n<h3>5.3 DiaryDetail Component<\/h3>\n<p>This component shows the details of the selected diary entry.<\/p>\n<pre><code>import React from 'react';\n\nconst DiaryDetail = ({ diary }) => {\n    return (\n        <div>\n            <h2>{diary.title}<\/h2>\n            <p>{diary.content}<\/p>\n        <\/div>\n    );\n};\n\nexport default DiaryDetail;<\/code><\/pre>\n<h3>5.4 DiaryForm Component<\/h3>\n<p>This is the form for writing a new diary entry or editing an existing one.<\/p>\n<pre><code>import React, { useState } from 'react';\n\nconst DiaryForm = ({ onSubmit }) => {\n    const [title, setTitle] = useState('');\n    const [content, setContent] = useState('');\n\n    const handleSubmit = (e) => {\n        e.preventDefault();\n        onSubmit({ title, content });\n        setTitle('');\n        setContent('');\n    };\n\n    return (\n        <form onSubmit=\"{handleSubmit}\">\n            <input onChange=\"{(e) => setTitle(e.target.value)}\" placeholder=\"Title\" type=\"text\" value=\"{title}\" required \/>\n            <textarea onChange=\"{(e) => setContent(e.target.value)}\" placeholder=\"Content\" value=\"{content}\" required><\/textarea>\n            <button type=\"submit\">Save<\/button>\n        <\/form>\n    );\n};\n\nexport default DiaryForm;<\/code><\/pre>\n<h2>6. Setting Up the Router<\/h2>\n<p>Now let&#8217;s set up the router in the `App.js` file. This file defines the routing logic for the entire application.<\/p>\n<pre><code>import React, { useState } from 'react';\nimport { BrowserRouter as Router, Route, Switch } from 'react-router-dom';\nimport Header from '.\/components\/Header';\nimport DiaryList from '.\/components\/DiaryList';\nimport DiaryDetail from '.\/components\/DiaryDetail';\nimport DiaryForm from '.\/components\/DiaryForm';\n\nconst App = () => {\n    const [diaries, setDiaries] = useState([]);\n\n    const addDiary = (diary) => {\n        setDiaries(prevDiaries => [...prevDiaries, { id: prevDiaries.length, ...diary }]);\n    };\n\n    return (\n        <Router>\n            <Header \/>\n            <Switch>\n                <Route exact path=\"\/\" render={() => <DiaryList diaries=\"{diaries}\" \/>} \/>\n                <Route path=\"\/diary\/new\" render={() => <DiaryForm onSubmit=\"{addDiary}\" \/>} \/>\n                <Route path=\"\/diary\/:id\" render={({ match }) => {\n                        const diary = diaries[match.params.id];\n                        return diary ? <DiaryDetail diary=\"{diary}\" \/> : <div>No diary entry found.<\/div>;\n                    }}\n                \/>\n            <\/Switch>\n        <\/Router>\n    );\n};\n\nexport default App;<\/code><\/pre>\n<h2>7. Dynamic Path Routing<\/h2>\n<p>Using React Router, we can implement dynamic path routing. In the example above, the path `\/diary\/:id` dynamically loads diary details based on the diary ID. At this time, `match.params.id` is used to retrieve the diary with that ID.<\/p>\n<h2>8. Running the Project<\/h2>\n<p>Once all of the above setup is complete, you can run the application using the command below.<\/p>\n<pre><code>npm start<\/code><\/pre>\n<p>Access the diary app by visiting <strong>http:\/\/localhost:3000<\/strong> in your browser.<\/p>\n<h2>9. Conclusion<\/h2>\n<p>In this course, we created a simple diary app using React. This app includes basic CRUD functionality as well as dynamic path routing features using React Router. Through this process, you have gained an understanding of React components and routing that can be applied to real application development.<\/p>\n<p>I hope this article has been helpful in your learning of React! In the next course, we will explore adding more features or other tools in the React ecosystem.<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello! In this course, we will create a basic diary app using React. This app will allow users to write, edit, and delete diary entries. Additionally, we will use React Router to add dynamic path routing, so that each diary entry can be accessed individually. Through this article, I will help you gain a deep &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32921\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;React Course: Diary App Example and Dynamic Route Routing with React Router&#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-32921","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 Course: Diary App Example and Dynamic Route Routing with React Router - \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\/32921\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Course: Diary App Example and Dynamic Route Routing with React Router - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Hello! In this course, we will create a basic diary app using React. This app will allow users to write, edit, and delete diary entries. Additionally, we will use React Router to add dynamic path routing, so that each diary entry can be accessed individually. Through this article, I will help you gain a deep &hellip; \ub354 \ubcf4\uae30 &quot;React Course: Diary App Example and Dynamic Route Routing with React Router&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32921\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:12:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:21:28+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\/32921\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32921\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"React Course: Diary App Example and Dynamic Route Routing with React Router\",\"datePublished\":\"2024-11-01T09:12:29+00:00\",\"dateModified\":\"2024-11-01T11:21:28+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32921\/\"},\"wordCount\":492,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"React basics course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32921\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32921\/\",\"name\":\"React Course: Diary App Example and Dynamic Route Routing with React Router - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:12:29+00:00\",\"dateModified\":\"2024-11-01T11:21:28+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32921\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32921\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32921\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Course: Diary App Example and Dynamic Route Routing with React Router\"}]},{\"@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 Course: Diary App Example and Dynamic Route Routing with React Router - \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\/32921\/","og_locale":"ko_KR","og_type":"article","og_title":"React Course: Diary App Example and Dynamic Route Routing with React Router - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Hello! In this course, we will create a basic diary app using React. This app will allow users to write, edit, and delete diary entries. Additionally, we will use React Router to add dynamic path routing, so that each diary entry can be accessed individually. Through this article, I will help you gain a deep &hellip; \ub354 \ubcf4\uae30 \"React Course: Diary App Example and Dynamic Route Routing with React Router\"","og_url":"https:\/\/atmokpo.com\/w\/32921\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:12:29+00:00","article_modified_time":"2024-11-01T11:21:28+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\/32921\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32921\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"React Course: Diary App Example and Dynamic Route Routing with React Router","datePublished":"2024-11-01T09:12:29+00:00","dateModified":"2024-11-01T11:21:28+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32921\/"},"wordCount":492,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["React basics course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32921\/","url":"https:\/\/atmokpo.com\/w\/32921\/","name":"React Course: Diary App Example and Dynamic Route Routing with React Router - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:12:29+00:00","dateModified":"2024-11-01T11:21:28+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32921\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32921\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32921\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"React Course: Diary App Example and Dynamic Route Routing with React Router"}]},{"@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\/32921","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=32921"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32921\/revisions"}],"predecessor-version":[{"id":32922,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32921\/revisions\/32922"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}