{"id":32951,"date":"2024-11-01T09:12:42","date_gmt":"2024-11-01T09:12:42","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32951"},"modified":"2024-11-01T11:21:20","modified_gmt":"2024-11-01T11:21:20","slug":"react-lecture-supplying-data-to-the-component-tree-and-refactoring-a-to-do-app-with-context","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32951\/","title":{"rendered":"React Lecture: Supplying Data to the Component Tree and Refactoring a To Do App with Context"},"content":{"rendered":"<p><body><\/p>\n<p>React is one of the most popular libraries for building modern web applications. This is due to its component-based architecture, reusability, and the ease of managing complex UIs. In this tutorial, we will explore how to supply data to the component tree using React&#8217;s Context API and how to refactor it to create a simple Todo application.<\/p>\n<h2>1. Basic Concepts of React<\/h2>\n<p>React is a collection of components that make up the user interface. Components have state and props, which determine how the UI is rendered.<\/p>\n<h3>1.1 State and Props<\/h3>\n<p>State represents the internal data of a component, which can change based on user input or network requests. On the other hand, props are the data passed from a parent component to a child component.<\/p>\n<h3>1.2 Component Tree<\/h3>\n<p>A React application is arranged in a tree structure made up of multiple components. This tree structure manages the flow of data from parent to child components.<\/p>\n<h2>2. Introduction to Context API<\/h2>\n<p>The Context API is used in React to pass data to components deeply nested within the component tree. It allows data to be supplied without having to pass props through multiple levels, helping to reduce the complexity of prop drilling.<\/p>\n<h3>2.1 Creating Context<\/h3>\n<p>To create Context, you first need to use React&#8217;s <code>createContext<\/code> function.<\/p>\n<pre><code>import React, { createContext, useContext, useState } from 'react';\n\nconst MyContext = createContext(); \/\/ Create Context<\/code><\/pre>\n<h3>2.2 Provider and Consumer<\/h3>\n<p>Use the Provider component of the Context to pass data down to child components. The Provider supplies data that can be accessed by all descendant components.<\/p>\n<pre><code>&lt;MyContext.Provider value={\/* context value *\/}&gt;\n    {\/* children *\/}\n&lt;\/MyContext.Provider&gt;<\/code><\/pre>\n<h3>2.3 Using Context<\/h3>\n<p>To use Context in a child component, you retrieve the data using the <code>useContext<\/code> hook.<\/p>\n<pre><code>const value = useContext(MyContext); \/\/ Use Context<\/code><\/pre>\n<h2>3. Setting Up the Todo App Structure<\/h2>\n<p>Now, let&#8217;s create a simple Todo app using Context API. First, we will set up the basic component structure.<\/p>\n<h3>3.1 Basic Component Structure<\/h3>\n<p>The Todo app consists of the following components.<\/p>\n<ul>\n<li><strong>App<\/strong>: The component that wraps the entire app<\/li>\n<li><strong>TodoProvider<\/strong>: The Provider component that manages the state of the Todo list<\/li>\n<li><strong>TodoList<\/strong>: The component that renders the list of Todo items<\/li>\n<li><strong>TodoItem<\/strong>: The component that renders individual Todo items<\/li>\n<li><strong>AddTodo<\/strong>: The component for adding new Todo items<\/li>\n<\/ul>\n<h3>3.2 Writing the App Code<\/h3>\n<p>First, we will write the <code>TodoProvider<\/code> component to manage the state of the Todo list.<\/p>\n<pre><code>const TodoProvider = ({ children }) =&gt; {\n    const [todos, setTodos] = useState([]);\n\n    const addTodo = (todo) =&gt; {\n        setTodos([...todos, todo]);\n    };\n\n    const removeTodo = (id) =&gt; {\n        setTodos(todos.filter(todo =&gt; todo.id !== id));\n    };\n\n    return (\n        &lt;MyContext.Provider value={{ todos, addTodo, removeTodo }}&gt;\n            {children}\n        &lt;\/MyContext.Provider&gt;\n    );\n};<\/code><\/pre>\n<p>Then we will write the <code>TodoList<\/code> and <code>AddTodo<\/code> components.<\/p>\n<pre><code>const TodoList = () =&gt; {\n    const { todos, removeTodo } = useContext(MyContext);\n\n    return (\n        &lt;ul&gt;\n            {todos.map(todo =&gt; (\n                &lt;TodoItem key={todo.id} todo={todo} onRemove={removeTodo} \/&gt;\n            ))}&lt;\/ul&gt;\n    );\n};\n\nconst AddTodo = () =&gt; {\n    const { addTodo } = useContext(MyContext);\n    const [inputValue, setInputValue] = useState('');\n\n    const handleSubmit = (e) =&gt; {\n        e.preventDefault();\n        if (!inputValue) return;\n        \n        addTodo({ id: Date.now(), text: inputValue });\n        setInputValue('');\n    };\n\n    return (\n        &lt;form onSubmit={handleSubmit}&gt;\n            &lt;input \n                type=\"text\" \n                value={inputValue} \n                onChange={(e) =&gt; setInputValue(e.target.value)} \n                placeholder=\"Enter new Todo\" \n            \/&gt;\n            &lt;button type=\"submit\"&gt;Add&lt;\/button&gt;\n        &lt;\/form&gt;\n    );\n};<\/code><\/pre>\n<h3>3.3 Final App Structure<\/h3>\n<p>Finally, we will use <code>TodoProvider<\/code> in the <code>App<\/code> component to wrap the other components.<\/p>\n<pre><code>const App = () =&gt; {\n    return (\n        &lt;TodoProvider&gt;\n            &lt;h1&gt;Todo List&lt;\/h1&gt;\n            &lt;AddTodo \/&gt;\n            &lt;TodoList \/&gt;\n        &lt;\/TodoProvider&gt;\n    );\n};<\/code><\/pre>\n<h2>4. State Management and Refactoring<\/h2>\n<p>State management is a crucial part of React applications. It helps clarify the flow and relationships of data later on. By using the Context API, child components can access the state directly for more efficient management.<\/p>\n<h3>4.1 State Management Patterns<\/h3>\n<p>There are various patterns for state management. When using it with the Context API, components must be designed to subscribe to it. This minimizes unnecessary re-renders.<\/p>\n<h3>4.2 Example of Refactoring<\/h3>\n<p>Refactoring allows for safely implementing additional features. For instance, let&#8217;s add logic to handle the completion status of Todo items.<\/p>\n<pre><code>const toggleTodo = (id) =&gt; {\n    setTodos(todos.map(todo =&gt; todo.id === id ? { ...todo, completed: !todo.completed } : todo));\n};<\/code><\/pre>\n<h3>4.3 Visualizing Completed Todo Items<\/h3>\n<p>To visually represent completed Todo items, we will modify the <code>TodoItem<\/code> component.<\/p>\n<pre><code>const TodoItem = ({ todo, onRemove }) =&gt; {\n    return (\n        &lt;li style={{ textDecoration: todo.completed ? 'line-through' : 'none' }}&gt;\n            {todo.text}\n            &lt;button onClick={() =&gt; onRemove(todo.id)}&gt;Delete&lt;\/button&gt;\n        &lt;\/li&gt;\n    );\n};<\/code><\/pre>\n<h2>5. Conclusion<\/h2>\n<p>In this tutorial, we learned how to use React&#8217;s Context API to supply data to the component tree and how to refactor a Todo application. Context makes it easy to share data between components and helps to keep the app&#8217;s structure cleaner.<\/p>\n<p>Understanding state management and data flow using React will greatly aid in building complex applications in the future. Now, I encourage you to use the Context API to create various applications. Thank you!<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>React is one of the most popular libraries for building modern web applications. This is due to its component-based architecture, reusability, and the ease of managing complex UIs. In this tutorial, we will explore how to supply data to the component tree using React&#8217;s Context API and how to refactor it to create a simple &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32951\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;React Lecture: Supplying Data to the Component Tree and Refactoring a To Do App with Context&#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-32951","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: Supplying Data to the Component Tree and Refactoring a To Do App with Context - \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\/32951\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Lecture: Supplying Data to the Component Tree and Refactoring a To Do App with Context - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"React is one of the most popular libraries for building modern web applications. This is due to its component-based architecture, reusability, and the ease of managing complex UIs. In this tutorial, we will explore how to supply data to the component tree using React&#8217;s Context API and how to refactor it to create a simple &hellip; \ub354 \ubcf4\uae30 &quot;React Lecture: Supplying Data to the Component Tree and Refactoring a To Do App with Context&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32951\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:12:42+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:21:20+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\/32951\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32951\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"React Lecture: Supplying Data to the Component Tree and Refactoring a To Do App with Context\",\"datePublished\":\"2024-11-01T09:12:42+00:00\",\"dateModified\":\"2024-11-01T11:21:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32951\/\"},\"wordCount\":580,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"React basics course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32951\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32951\/\",\"name\":\"React Lecture: Supplying Data to the Component Tree and Refactoring a To Do App with Context - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:12:42+00:00\",\"dateModified\":\"2024-11-01T11:21:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32951\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32951\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32951\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Lecture: Supplying Data to the Component Tree and Refactoring a To Do App with Context\"}]},{\"@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: Supplying Data to the Component Tree and Refactoring a To Do App with Context - \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\/32951\/","og_locale":"ko_KR","og_type":"article","og_title":"React Lecture: Supplying Data to the Component Tree and Refactoring a To Do App with Context - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"React is one of the most popular libraries for building modern web applications. This is due to its component-based architecture, reusability, and the ease of managing complex UIs. In this tutorial, we will explore how to supply data to the component tree using React&#8217;s Context API and how to refactor it to create a simple &hellip; \ub354 \ubcf4\uae30 \"React Lecture: Supplying Data to the Component Tree and Refactoring a To Do App with Context\"","og_url":"https:\/\/atmokpo.com\/w\/32951\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:12:42+00:00","article_modified_time":"2024-11-01T11:21:20+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\/32951\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32951\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"React Lecture: Supplying Data to the Component Tree and Refactoring a To Do App with Context","datePublished":"2024-11-01T09:12:42+00:00","dateModified":"2024-11-01T11:21:20+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32951\/"},"wordCount":580,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["React basics course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32951\/","url":"https:\/\/atmokpo.com\/w\/32951\/","name":"React Lecture: Supplying Data to the Component Tree and Refactoring a To Do App with Context - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:12:42+00:00","dateModified":"2024-11-01T11:21:20+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32951\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32951\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32951\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"React Lecture: Supplying Data to the Component Tree and Refactoring a To Do App with Context"}]},{"@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\/32951","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=32951"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32951\/revisions"}],"predecessor-version":[{"id":32952,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32951\/revisions\/32952"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32951"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}