{"id":32949,"date":"2024-11-01T09:12:41","date_gmt":"2024-11-01T09:12:41","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32949"},"modified":"2024-11-01T11:21:21","modified_gmt":"2024-11-01T11:21:21","slug":"react-course-supplying-data-to-the-component-tree-context","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32949\/","title":{"rendered":"React Course: Supplying Data to the Component Tree, Context"},"content":{"rendered":"<p><body><\/p>\n<article>\n<header>\n<p>React is a JavaScript library used for building user interfaces (UIs) based on components. In this course, we will delve into how to use React&#8217;s Context API to supply data to the component tree. Context is one of React&#8217;s powerful features that helps in efficiently passing data between components.<\/p>\n<\/header>\n<section>\n<h2>1. What is Context API?<\/h2>\n<p>The Context API is a tool that allows data to be passed to components that do not require it in React. It is useful primarily for avoiding props drilling. By using Context, data can be transmitted without having to pass props directly from parent to child components.<\/p>\n<p>Context is primarily useful for global state management or sharing data required by multiple child elements. For example, user authentication information, UI language settings, theme information, etc., can be managed using Context.<\/p>\n<\/section>\n<section>\n<h2>2. Components of Context API<\/h2>\n<p>The Context API consists of the following main components:<\/p>\n<ul>\n<li><strong>Creating Context<\/strong>: Use the <code>React.createContext()<\/code> function to create Context.<\/li>\n<li><strong>Provider Component<\/strong>: Use the Provider component of the Context to supply data to child components.<\/li>\n<li><strong>Consumer Component<\/strong>: This is the Consumer component for subscribing to data in child components. This technique uses the render props pattern.<\/li>\n<li><strong>Hooks<\/strong>: Use the <code>useContext<\/code> hook to utilize Context in functional components.<\/li>\n<\/ul>\n<\/section>\n<section>\n<h2>3. Creating and Using Context<\/h2>\n<p>Let\u2019s first look at how to create Context. The following example explains how to create and use a simple Context:<\/p>\n<pre><code>\nimport React, { createContext, useContext, useState } from 'react';\n\n\/\/ 3.1. Creating Context\nconst MyContext = createContext();\n\n\/\/ 3.2. Creating Provider component\nconst MyProvider = ({ children }) =&gt; {\n    const [value, setValue] = useState('Hello, React!');\n\n    return (\n        <mycontext.provider setvalue=\"\" value=\"{{\" value,=\"\" }}=\"\">\n            {children}\n        <\/mycontext.provider>\n    );\n};\n\n\/\/ 3.3. Example of using Consumer\nconst MyComponent = () =&gt; {\n    const { value } = useContext(MyContext);\n    return <h1>{value}<\/h1>;\n};\n\n\/\/ 3.4. App component\nconst App = () =&gt; (\n    <myprovider>\n        <mycomponent><\/mycomponent>\n    <\/myprovider>\n);\n            <\/code><\/pre>\n<p>In this example, we create <code>MyContext<\/code> and supply data to child components through <code>MyProvider<\/code>. In <code>MyComponent<\/code>, we use the <code>useContext<\/code> hook to access the value of the Context.<\/p>\n<\/section>\n<section>\n<h2>4. Advanced Usage of React Context<\/h2>\n<p>When using the Context API, there are several considerations beyond the basic usage. In this section, we will discuss various strategies for using Context more effectively.<\/p>\n<h3>4.1. Using Multiple Contexts<\/h3>\n<p>It is possible to use multiple Contexts within a single application. Splitting different data into their respective Contexts makes the code cleaner and improves maintainability. For example, user settings, theme settings, and language settings can be managed in different Contexts.<\/p>\n<pre><code>\nconst ThemeContext = createContext();\nconst LanguageContext = createContext();\n            <\/code><\/pre>\n<h3>4.2. Performance Optimization of Context<\/h3>\n<p>Using Context can potentially degrade performance. When Context changes, all child components re-render. To mitigate this, Context can be divided into finer-grained contexts or memoization can be used.<\/p>\n<pre><code>\nconst MyComponent = React.memo(() =&gt; {\n    const { value } = useContext(MyContext);\n    return <h1>{value}<\/h1>;\n});\n            <\/code><\/pre>\n<h3>4.3. Comparing Context and Redux<\/h3>\n<p>Both Context API and Redux are methods for global state management. However, Redux provides patterns for managing complex state along with additional features like middleware. For simple state management, the Context API is suitable, but as the application grows in complexity, considering Redux is advisable.<\/p>\n<\/section>\n<section>\n<h2>5. Practical Example with React<\/h2>\n<p>Now, let&#8217;s practice using the Context API. Below is a simple example that manages user authentication status.<\/p>\n<pre><code>\nconst AuthContext = createContext();\n\nconst AuthProvider = ({ children }) =&gt; {\n    const [isAuthenticated, setIsAuthenticated] = useState(false);\n\n    const login = () =&gt; setIsAuthenticated(true);\n    const logout = () =&gt; setIsAuthenticated(false);\n\n    return (\n        <authcontext.provider isauthenticated,=\"\" login,=\"\" logout=\"\" value=\"{{\" }}=\"\">\n            {children}\n        <\/authcontext.provider>\n    );\n};\n\nconst LoginButton = () =&gt; {\n    const { login } = useContext(AuthContext);\n    return <button onclick=\"{login}\">Login<\/button>;\n};\n\nconst LogoutButton = () =&gt; {\n    const { logout } = useContext(AuthContext);\n    return <button onclick=\"{logout}\">Logout<\/button>;\n};\n\nconst AuthStatus = () =&gt; {\n    const { isAuthenticated } = useContext(AuthContext);\n    return <h2>{isAuthenticated ? 'You are logged in' : 'Please log in'}<\/h2>;\n};\n\nconst App = () =&gt; (\n    <authprovider>\n        <authstatus><\/authstatus>\n        <loginbutton><\/loginbutton>\n        <logoutbutton><\/logoutbutton>\n    <\/authprovider>\n);\n            <\/code><\/pre>\n<p>In the above example, we created <code>AuthContext<\/code> to manage user authentication status, and the related components provide login and logout functionality through Context.<\/p>\n<\/section>\n<section>\n<h2>6. Conclusion<\/h2>\n<p>The Context API is a powerful tool that helps in efficiently passing data between components in React. It can be utilized in a variety of scenarios, from simple data transmission to complex state management. By considering performance issues and using it appropriately, you can significantly improve development efficiency. I hope this course has been helpful for your React development journey.<\/p>\n<\/section>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>React is a JavaScript library used for building user interfaces (UIs) based on components. In this course, we will delve into how to use React&#8217;s Context API to supply data to the component tree. Context is one of React&#8217;s powerful features that helps in efficiently passing data between components. 1. What is Context API? The &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32949\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;React Course: Supplying Data to the Component Tree, 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-32949","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: Supplying Data to the Component Tree, 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\/32949\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Course: Supplying Data to the Component Tree, Context - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"React is a JavaScript library used for building user interfaces (UIs) based on components. In this course, we will delve into how to use React&#8217;s Context API to supply data to the component tree. Context is one of React&#8217;s powerful features that helps in efficiently passing data between components. 1. What is Context API? The &hellip; \ub354 \ubcf4\uae30 &quot;React Course: Supplying Data to the Component Tree, Context&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32949\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:12:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:21:21+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\/32949\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32949\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"React Course: Supplying Data to the Component Tree, Context\",\"datePublished\":\"2024-11-01T09:12:41+00:00\",\"dateModified\":\"2024-11-01T11:21:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32949\/\"},\"wordCount\":519,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"React basics course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32949\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32949\/\",\"name\":\"React Course: Supplying Data to the Component Tree, Context - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:12:41+00:00\",\"dateModified\":\"2024-11-01T11:21:21+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32949\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32949\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32949\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Course: Supplying Data to the Component Tree, 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 Course: Supplying Data to the Component Tree, 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\/32949\/","og_locale":"ko_KR","og_type":"article","og_title":"React Course: Supplying Data to the Component Tree, Context - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"React is a JavaScript library used for building user interfaces (UIs) based on components. In this course, we will delve into how to use React&#8217;s Context API to supply data to the component tree. Context is one of React&#8217;s powerful features that helps in efficiently passing data between components. 1. What is Context API? The &hellip; \ub354 \ubcf4\uae30 \"React Course: Supplying Data to the Component Tree, Context\"","og_url":"https:\/\/atmokpo.com\/w\/32949\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:12:41+00:00","article_modified_time":"2024-11-01T11:21:21+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\/32949\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32949\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"React Course: Supplying Data to the Component Tree, Context","datePublished":"2024-11-01T09:12:41+00:00","dateModified":"2024-11-01T11:21:21+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32949\/"},"wordCount":519,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["React basics course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32949\/","url":"https:\/\/atmokpo.com\/w\/32949\/","name":"React Course: Supplying Data to the Component Tree, Context - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:12:41+00:00","dateModified":"2024-11-01T11:21:21+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32949\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32949\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32949\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"React Course: Supplying Data to the Component Tree, 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\/32949","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=32949"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32949\/revisions"}],"predecessor-version":[{"id":32950,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32949\/revisions\/32950"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32949"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32949"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32949"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}