{"id":32953,"date":"2024-11-01T09:12:43","date_gmt":"2024-11-01T09:12:43","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32953"},"modified":"2024-11-01T11:21:20","modified_gmt":"2024-11-01T11:21:20","slug":"react-course-passing-values-to-components","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32953\/","title":{"rendered":"React Course: Passing Values to Components"},"content":{"rendered":"<p><body><\/p>\n<article>\n<p>\n            React is one of the JavaScript libraries that is effective for building user interfaces. In particular, thanks to its component-based architecture, applications can be developed in a reusable and maintainable way. In this course, we will explore in depth how to pass data between components in React.\n        <\/p>\n<h2>1. Basic Concepts of React<\/h2>\n<p>\n            React is composed of independent UI pieces called <strong>components<\/strong>, and these components come together to form complex user interfaces. Components can be broadly divided into two types: <strong>functional components<\/strong> and <strong>class components<\/strong>.\n        <\/p>\n<h3>1.1 Functional Components<\/h3>\n<p>\n            Functional components are the latest trend in React and can be written very simply. Here is an example of a basic functional component:\n        <\/p>\n<pre><code>\nfunction Greeting() {\n    return &lt;h1&gt;Hello!&lt;\/h1&gt;;\n}\n        <\/code><\/pre>\n<h3>1.2 Class Components<\/h3>\n<p>\n            Class components are defined using JavaScript classes. Below is an example using a class:\n        <\/p>\n<pre><code>\nclass Greeting extends React.Component {\n    render() {\n        return &lt;h1&gt;Hello!&lt;\/h1&gt;;\n    }\n}\n        <\/code><\/pre>\n<h2>2. Passing Values Between Components<\/h2>\n<p>\n            The most common way to pass data between components is to use <strong>props<\/strong>. Props are objects used to pass data from a parent component to a child component.\n        <\/p>\n<h3>2.1 Definition and Use of Props<\/h3>\n<p>\n            Props are the properties of a component that primarily control the rendering of the child component. First, let&#8217;s look at a basic example of passing props from a parent component to a child component.\n        <\/p>\n<pre><code>\nfunction ParentComponent() {\n    const message = \"Hello! This is React.\";\n    return &lt;ChildComponent message={message} \/&gt;;\n}\n\nfunction ChildComponent(props) {\n    return &lt;p&gt;{props.message}&lt;\/p&gt;;\n}\n        <\/code><\/pre>\n<p>\n            In the example above, <code>ParentComponent<\/code> is passing a prop called <code>message<\/code> to <code>ChildComponent<\/code>. The child component can access this value through <code>props.message<\/code>.\n        <\/p>\n<h3>2.2 Props Type Checking<\/h3>\n<p>\n            In React, <strong>PropTypes<\/strong> can be used to check the data types of the props being passed. This enhances code safety and makes debugging easier. For example, it can be used as follows:\n        <\/p>\n<pre><code>\nimport PropTypes from 'prop-types';\n\nChildComponent.propTypes = {\n    message: PropTypes.string.isRequired,\n};\n        <\/code><\/pre>\n<p>\n            In the example above, <code>ChildComponent<\/code> specifies that <code>message<\/code> must be of type string and is required.\n        <\/p>\n<h2>3. Various Patterns for Data Transfer Between Components<\/h2>\n<p>\n            In React, data can be passed not only using props but also through several other patterns. This section will examine these cases.\n        <\/p>\n<h3>3.1 Data Transfer Between Parent and Child Components<\/h3>\n<p>\n            A parent component can pass data to a child component using props. This method ensures a unidirectional data flow and is a good structure for managing state.\n        <\/p>\n<pre><code>\nfunction App() {\n    const [count, setCount] = useState(0);\n\n    return (\n        &lt;div&gt;\n            &lt;Counter count={count} \/&gt;\n            &lt;button onClick={() =&gt; setCount(count + 1)}&gt;Increase&lt;\/button&gt;\n        &lt;\/div&gt;\n    );\n}\n\nfunction Counter(props) {\n    return &lt;p&gt;Current count: {props.count}&lt;\/p&gt;;\n}\n        <\/code><\/pre>\n<h3>3.2 Data Transfer from Child to Parent Components<\/h3>\n<p>\n            To pass data from a child component to a parent component, it is common to pass a callback function as a prop from the parent to the child.\n        <\/p>\n<pre><code>\nfunction ParentComponent() {\n    const handleChildData = (data) =&gt; {\n        console.log(data);\n    };\n\n    return &lt;ChildComponent onSendData={handleChildData} \/&gt;;\n}\n\nfunction ChildComponent(props) {\n    const sendData = () =&gt; {\n        props.onSendData(\"Data sent from child component!\");\n    };\n\n    return &lt;button onClick={sendData}&gt;Send Data&lt;\/button&gt;;\n}\n        <\/code><\/pre>\n<h3>3.3 Data Transfer Using Context API<\/h3>\n<p>\n            When you need to pass data across multiple components, using the <strong>Context API<\/strong> can be efficient. The Context API is a form of global state management that allows components in the tree to access data without passing it down through props.\n        <\/p>\n<pre><code>\nimport React, { createContext, useContext } from 'react';\n\nconst MyContext = createContext();\n\nfunction App() {\n    return (\n        &lt;MyContext.Provider value={\"Global Data\"}&gt;\n            &lt;ChildComponent \/&gt;\n        &lt;\/MyContext.Provider&gt;\n    );\n}\n\nfunction ChildComponent() {\n    const data = useContext(MyContext);\n    return &lt;p&gt;{data}&lt;\/p&gt;;\n}\n        <\/code><\/pre>\n<h2>4. Conclusion<\/h2>\n<p>\n            Passing data between components in React is a core concept. In this course, we explored methods for data transfer using props and various patterns. By selecting the appropriate method for each scenario and applying structured programming practices, we can build efficient React applications. Mastering this concept while learning React will greatly aid in handling complex applications.\n        <\/p>\n<h2>5. References<\/h2>\n<ul>\n<li><a href=\"https:\/\/reactjs.org\/docs\/getting-started.html\">Official React Documentation<\/a><\/li>\n<li><a href=\"https:\/\/reactjs.org\/docs\/components-and-props.html\">Components and Props<\/a><\/li>\n<li><a href=\"https:\/\/reactjs.org\/docs\/context.html\">Context API<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/facebook\/prop-types\">PropTypes<\/a><\/li>\n<\/ul>\n<\/article>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>React is one of the JavaScript libraries that is effective for building user interfaces. In particular, thanks to its component-based architecture, applications can be developed in a reusable and maintainable way. In this course, we will explore in depth how to pass data between components in React. 1. Basic Concepts of React React is composed &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32953\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;React Course: Passing Values to Components&#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-32953","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: Passing Values to Components - \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\/32953\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Course: Passing Values to Components - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"React is one of the JavaScript libraries that is effective for building user interfaces. In particular, thanks to its component-based architecture, applications can be developed in a reusable and maintainable way. In this course, we will explore in depth how to pass data between components in React. 1. Basic Concepts of React React is composed &hellip; \ub354 \ubcf4\uae30 &quot;React Course: Passing Values to Components&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32953\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:12:43+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=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32953\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32953\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"React Course: Passing Values to Components\",\"datePublished\":\"2024-11-01T09:12:43+00:00\",\"dateModified\":\"2024-11-01T11:21:20+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32953\/\"},\"wordCount\":480,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"React basics course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32953\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32953\/\",\"name\":\"React Course: Passing Values to Components - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:12:43+00:00\",\"dateModified\":\"2024-11-01T11:21:20+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32953\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32953\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32953\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Course: Passing Values to Components\"}]},{\"@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: Passing Values to Components - \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\/32953\/","og_locale":"ko_KR","og_type":"article","og_title":"React Course: Passing Values to Components - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"React is one of the JavaScript libraries that is effective for building user interfaces. In particular, thanks to its component-based architecture, applications can be developed in a reusable and maintainable way. In this course, we will explore in depth how to pass data between components in React. 1. Basic Concepts of React React is composed &hellip; \ub354 \ubcf4\uae30 \"React Course: Passing Values to Components\"","og_url":"https:\/\/atmokpo.com\/w\/32953\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:12:43+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":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32953\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32953\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"React Course: Passing Values to Components","datePublished":"2024-11-01T09:12:43+00:00","dateModified":"2024-11-01T11:21:20+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32953\/"},"wordCount":480,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["React basics course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32953\/","url":"https:\/\/atmokpo.com\/w\/32953\/","name":"React Course: Passing Values to Components - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:12:43+00:00","dateModified":"2024-11-01T11:21:20+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32953\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32953\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32953\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"React Course: Passing Values to Components"}]},{"@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\/32953","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=32953"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32953\/revisions"}],"predecessor-version":[{"id":32954,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32953\/revisions\/32954"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}