{"id":32875,"date":"2024-11-01T09:12:08","date_gmt":"2024-11-01T09:12:08","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32875"},"modified":"2024-11-01T11:21:39","modified_gmt":"2024-11-01T11:21:39","slug":"react-course-loops","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32875\/","title":{"rendered":"React Course: Loops"},"content":{"rendered":"<p>React is a JavaScript library for building user interfaces (UI) that helps developers efficiently design complex UIs through a component-based architecture. In this course, we will delve deeply into the loops commonly used in React. Specifically, we will explore how to use loops within JSX, the main types of loops, and practical examples through sample projects.<\/p>\n<h2>1. What is a Loop?<\/h2>\n<p>A loop is a programming structure that allows a specific task to be performed multiple times, used to repeat the same behavior for various data. The loops provided by JavaScript include <code>for<\/code>, <code>while<\/code>, <code>forEach<\/code>, and others. In React, they are mainly used to render array data repeatedly.<\/p>\n<h2>2. JSX and Loops<\/h2>\n<p>JSX (JavaScript XML) is a syntax extension for JavaScript that allows HTML tags to be written directly within JavaScript code. However, typical JavaScript loops cannot be used directly in JSX, so the JavaScript <code>map()<\/code> method is primarily used. The <code>map()<\/code> method executes a function on each element of an array and returns a new array as a result.<\/p>\n<h3>2.1. Using Loops with the map() Method<\/h3>\n<p>Let&#8217;s look at an example of a loop using the <code>map()<\/code> method that is frequently used in React.<\/p>\n<pre><code>const items = ['Apple', 'Banana', 'Cherry'];\n\nconst ItemList = () =&gt; {\n    return (\n        &lt;ul&gt;\n            {items.map(item =&gt; &lt;li key={item}&gt;{item}&lt;\/li&gt;)}\n        &lt;\/ul&gt;\n    );\n};<\/code><\/pre>\n<p>In the code above, we are iterating through each element of the <code>items<\/code> array and converting it into list tags. At this point, we add a <code>key<\/code> attribute to each list item to optimize performance. React helps to efficiently handle changes by comparing arrays.<\/p>\n<h2>3. Types of Loops<\/h2>\n<p>Let&#8217;s explore various types of loops that can be used in React components.<\/p>\n<h3>3.1. For Loop<\/h3>\n<p>Although the traditional <code>for<\/code> loop cannot be used directly in JSX, it can be effectively utilized in general business logic. For example, we can use a <code>for<\/code> loop to create an array and then output it as follows.<\/p>\n<pre><code>const ItemList = () =&gt; {\n    const items = [];\n    for(let i = 1; i &lt;= 5; i++) {\n        items.push(`Item ${i}`);\n    }\n    return (\n        &lt;ul&gt;\n            {items.map(item =&gt; &lt;li key={item}&gt;{item}&lt;\/li&gt;)}\n        &lt;\/ul&gt;\n    );\n};<\/code><\/pre>\n<h3>3.2. While Loop<\/h3>\n<p>The <code>while<\/code> loop also cannot be used directly in JSX but is useful for state management. For example, it can be used as follows.<\/p>\n<pre><code>const ItemList = () =&gt; {\n    const items = [];\n    let i = 1;\n    while(i &lt;= 5) {\n        items.push(`Item ${i}`);\n        i++;\n    }\n    return (\n        &lt;ul&gt;\n            {items.map(item =&gt; &lt;li key={item}&gt;{item}&lt;\/li&gt;)}\n        &lt;\/ul&gt;\n    );\n};<\/code><\/pre>\n<h2>4. Conditional Rendering and Loops<\/h2>\n<p>In React, conditional rendering can be used in conjunction with loops. For instance, let&#8217;s examine how to render array elements only when they satisfy a specific condition.<\/p>\n<pre><code>const numbers = [1, 2, 3, 4, 5];\n\nconst ItemList = () =&gt; {\n    return (\n        &lt;ul&gt;\n            {numbers.map(number =&gt; number % 2 === 0 ? &lt;li key={number}&gt;{number}&lt;\/li&gt; : null)}\n        &lt;\/ul&gt;\n    );\n};<\/code><\/pre>\n<h2>5. Practical Application Using Loops<\/h2>\n<p>In this section, we will create a simple Todo List application using loops. This application will manage a list of tasks input by the user as state and render the added items through loops.<\/p>\n<pre><code>import React, { useState } from 'react';\n\nconst TodoApp = () =&gt; {\n    const [todos, setTodos] = useState([]);\n    const [inputValue, setInputValue] = useState('');\n\n    const addTodo = () =&gt; {\n        if (inputValue.trim() !== '') {\n            setTodos([...todos, inputValue]);\n            setInputValue('');\n        }\n    };\n\n    return (\n        &lt;div&gt;\n            &lt;input \n                type=\"text\"\n                value={inputValue}\n                onChange={(e) =&gt; setInputValue(e.target.value)} \n            \/&gt;\n            &lt;button onClick={addTodo}&gt;Add&lt;\/button&gt;\n            &lt;ul&gt;\n                {todos.map((todo, index) =&gt; &lt;li key={index}&gt;{todo}&lt;\/li&gt;)}\n            &lt;\/ul&gt;\n        &lt;\/div&gt;\n    );\n};<\/code><\/pre>\n<p>In the above example, we use the <code>useState<\/code> hook to manage user input and employ the <code>map()<\/code> method to render the added task list on the screen. By managing the input value as state, the UI is updated in real-time as seen by the user.<\/p>\n<h2>6. Conclusion<\/h2>\n<p>In this course, we explored various ways to use loops in React. Loops can render large amounts of data efficiently, significantly contributing to improving user experience. Additionally, we learned methods to effectively construct complex UIs by combining loops with conditional rendering. In the next course, we will delve deeper into React&#8217;s state management.<\/p>\n<p>I hope this course has been informative for learning React. I encourage you to continue mastering React through various practical exercises!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>React is a JavaScript library for building user interfaces (UI) that helps developers efficiently design complex UIs through a component-based architecture. In this course, we will delve deeply into the loops commonly used in React. Specifically, we will explore how to use loops within JSX, the main types of loops, and practical examples through sample &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32875\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;React Course: Loops&#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-32875","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: Loops - \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\/32875\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Course: Loops - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"React is a JavaScript library for building user interfaces (UI) that helps developers efficiently design complex UIs through a component-based architecture. In this course, we will delve deeply into the loops commonly used in React. Specifically, we will explore how to use loops within JSX, the main types of loops, and practical examples through sample &hellip; \ub354 \ubcf4\uae30 &quot;React Course: Loops&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32875\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:12:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:21:39+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=\"4\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32875\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32875\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"React Course: Loops\",\"datePublished\":\"2024-11-01T09:12:08+00:00\",\"dateModified\":\"2024-11-01T11:21:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32875\/\"},\"wordCount\":501,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"React basics course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32875\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32875\/\",\"name\":\"React Course: Loops - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:12:08+00:00\",\"dateModified\":\"2024-11-01T11:21:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32875\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32875\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32875\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Course: Loops\"}]},{\"@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: Loops - \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\/32875\/","og_locale":"ko_KR","og_type":"article","og_title":"React Course: Loops - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"React is a JavaScript library for building user interfaces (UI) that helps developers efficiently design complex UIs through a component-based architecture. In this course, we will delve deeply into the loops commonly used in React. Specifically, we will explore how to use loops within JSX, the main types of loops, and practical examples through sample &hellip; \ub354 \ubcf4\uae30 \"React Course: Loops\"","og_url":"https:\/\/atmokpo.com\/w\/32875\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:12:08+00:00","article_modified_time":"2024-11-01T11:21:39+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":"4\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32875\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32875\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"React Course: Loops","datePublished":"2024-11-01T09:12:08+00:00","dateModified":"2024-11-01T11:21:39+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32875\/"},"wordCount":501,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["React basics course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32875\/","url":"https:\/\/atmokpo.com\/w\/32875\/","name":"React Course: Loops - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:12:08+00:00","dateModified":"2024-11-01T11:21:39+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32875\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32875\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32875\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"React Course: Loops"}]},{"@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\/32875","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=32875"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32875\/revisions"}],"predecessor-version":[{"id":32876,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32875\/revisions\/32876"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32875"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32875"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32875"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}