{"id":32955,"date":"2024-11-01T09:12:44","date_gmt":"2024-11-01T09:12:44","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32955"},"modified":"2024-11-01T11:21:19","modified_gmt":"2024-11-01T11:21:19","slug":"react-course-components-and-state","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32955\/","title":{"rendered":"React Course: Components and State"},"content":{"rendered":"<p><body><\/p>\n<p>React is a JavaScript library for building user interfaces. React adopts a component-based architecture, allowing developers to create reusable UI components. This course will delve deeply into the core concepts of React: &#8216;components&#8217; and &#8216;state&#8217;.<\/p>\n<h2>1. What is a component?<\/h2>\n<p>A component is the basic building block of React, representing small pieces that make up the UI. Each component operates independently and can be combined with other components to form complex user interfaces.<\/p>\n<h3>1.1. Types of components<\/h3>\n<p>React components can be broadly classified into two types.<\/p>\n<ul>\n<li><strong>Class components:<\/strong> These are components defined using ES6 classes. They can manage state.<\/li>\n<li><strong>Functional components:<\/strong> These are components defined as JavaScript functions. Recently, a feature called Hooks has been introduced to allow for state management.<\/li>\n<\/ul>\n<h3>1.2. Creating a component<\/h3>\n<p>A React component can be created as follows.<\/p>\n<pre><code>import React from 'react';\n\nclass MyComponent extends React.Component {\n    render() {\n        return &lt;h1&gt;Hello, React!&lt;\/h1&gt;;\n    }\n}<\/code><\/pre>\n<p>The above example defines a component called &#8216;MyComponent&#8217; and uses it to render the text &#8220;Hello, React!&#8221; on the screen.<\/p>\n<h2>2. What is state?<\/h2>\n<p>State refers to the data or values of a component. The state of a component is stored internally, and when the state changes, the component is automatically re-rendered.<\/p>\n<h3>2.1. Using state<\/h3>\n<p>To use state in a class component, you define the initial state in the constructor method and use the `this.setState()` method to update the state.<\/p>\n<pre><code>class MyStateComponent extends React.Component {\n    constructor(props) {\n        super(props);\n        this.state = { count: 0 };\n    }\n    \n    increment = () =&gt; {\n        this.setState({ count: this.state.count + 1 });\n    }\n    \n    render() {\n        return (\n            &lt;div&gt;\n                &lt;p&gt;Current count: {this.state.count}&lt;\/p&gt;\n                &lt;button onClick={this.increment}&gt;Increase&lt;\/button&gt;\n            &lt;\/div&gt;\n        );\n    }\n}<\/code><\/pre>\n<p>In the example above, &#8216;MyStateComponent&#8217; is a component that has the functionality to increase the count. When the `increment` method is called, the state changes, causing the component to re-render.<\/p>\n<h3>2.2. Managing state in functional components<\/h3>\n<p>To manage state in functional components, you use React&#8217;s `useState` Hook. Below is an example of a functional component that uses state.<\/p>\n<pre><code>import React, { useState } from 'react';\n\nconst MyFunctionalComponent = () =&gt; {\n    const [count, setCount] = useState(0);\n    \n    const increment = () =&gt; {\n        setCount(count + 1);\n    }\n    \n    return (\n        &lt;div&gt;\n            &lt;p&gt;Current count: {count}&lt;\/p&gt;\n            &lt;button onClick={increment}&gt;Increase&lt;\/button&gt;\n        &lt;\/div&gt;\n    );\n}<\/code><\/pre>\n<h2>3. The impact of state on components<\/h2>\n<p>State is an important factor in dynamically changing the UI of a component. When the state changes, everything related to the component is re-rendered, providing a UI that reflects the latest state.<\/p>\n<h3>3.1. The difference between Props and State<\/h3>\n<p>&#8216;Props&#8217;, used for passing data between components, and &#8216;state&#8217;, used for managing data within a component, are distinct concepts. Props are read-only data passed from parent components to child components, while state is data managed internally by the component itself.<\/p>\n<h2>4. Managing components and state in React<\/h2>\n<p>A React application typically consists of multiple components, and the data flow between them occurs from parent components to child components. Below, we will explain how to effectively manage components and state.<\/p>\n<h3>4.1. Lifting state up<\/h3>\n<p>Lifting state up refers to managing state in a parent component and passing it down to child components via props. This allows multiple child components to share the same state, leading to a more consistent UI.<\/p>\n<pre><code>class ParentComponent extends React.Component {\n    constructor(props) {\n        super(props);\n        this.state = { sharedState: 0 };\n    }\n    \n    updateSharedState = (newValue) =&gt; {\n        this.setState({ sharedState: newValue });\n    }\n    \n    render() {\n        return (\n            &lt;div&gt;\n                &lt;ChildComponent sharedState={this.state.sharedState} updateSharedState={this.updateSharedState} \/&gt;\n            &lt;\/div&gt;\n        );\n    }\n}<\/code><\/pre>\n<h3>4.2. State management libraries<\/h3>\n<p>As projects grow larger and state management becomes more complex, it is advisable to use state management libraries such as Redux or MobX. These libraries are useful for managing global state and help simplify state management in complex applications.<\/p>\n<p>For example, when using Redux, you can manage state as follows.<\/p>\n<pre><code>import { createStore } from 'redux';\n\nconst initialState = { count: 0 };\n\nconst reducer = (state = initialState, action) =&gt; {\n    switch (action.type) {\n        case 'INCREMENT':\n            return { count: state.count + 1 };\n        default:\n            return state;\n    }\n};\n\nconst store = createStore(reducer);<\/code><\/pre>\n<h2>5. Lifecycle of React components<\/h2>\n<p>React components go through the processes of creation, updating, and removal, where specific methods are called at each stage. This is referred to as the &#8216;lifecycle&#8217;.<\/p>\n<h3>5.1. Lifecycle methods<\/h3>\n<p>Key lifecycle methods available in class components include the following.<\/p>\n<ul>\n<li><strong>componentDidMount:<\/strong> Called after the component has finished its first rendering.<\/li>\n<li><strong>componentDidUpdate:<\/strong> Called after the component has been updated.<\/li>\n<li><strong>componentWillUnmount:<\/strong> Called when the component is unmounted (removed).<\/li>\n<\/ul>\n<h2>6. Conclusion<\/h2>\n<p>In React, components and state play a crucial role in the structure and behavior of applications. By using components to structure the UI and state to provide a dynamic user experience, developers can create applications that satisfy end users. This course aims to help you understand the basic concepts of React, build various components, and learn how to manage state.<\/p>\n<h2>7. Additional resources<\/h2>\n<p>You can find more information through the official React documentation and various online courses. Below are some useful links:<\/p>\n<ul>\n<li><a href=\"https:\/\/reactjs.org\/docs\/getting-started.html\">Official React Documentation<\/a><\/li>\n<li><a href=\"https:\/\/egghead.io\/courses\/start-learning-react\">React Learning Course<\/a><\/li>\n<li><a href=\"https:\/\/www.udemy.com\/topic\/react\/\">Udemy React Courses<\/a><\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>React is a JavaScript library for building user interfaces. React adopts a component-based architecture, allowing developers to create reusable UI components. This course will delve deeply into the core concepts of React: &#8216;components&#8217; and &#8216;state&#8217;. 1. What is a component? A component is the basic building block of React, representing small pieces that make up &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32955\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;React Course: Components and State&#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-32955","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: Components and State - \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\/32955\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Course: Components and State - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"React is a JavaScript library for building user interfaces. React adopts a component-based architecture, allowing developers to create reusable UI components. This course will delve deeply into the core concepts of React: &#8216;components&#8217; and &#8216;state&#8217;. 1. What is a component? A component is the basic building block of React, representing small pieces that make up &hellip; \ub354 \ubcf4\uae30 &quot;React Course: Components and State&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32955\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:12:44+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:21:19+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\/32955\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32955\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"React Course: Components and State\",\"datePublished\":\"2024-11-01T09:12:44+00:00\",\"dateModified\":\"2024-11-01T11:21:19+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32955\/\"},\"wordCount\":645,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"React basics course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32955\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32955\/\",\"name\":\"React Course: Components and State - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:12:44+00:00\",\"dateModified\":\"2024-11-01T11:21:19+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32955\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32955\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32955\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Course: Components and State\"}]},{\"@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: Components and State - \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\/32955\/","og_locale":"ko_KR","og_type":"article","og_title":"React Course: Components and State - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"React is a JavaScript library for building user interfaces. React adopts a component-based architecture, allowing developers to create reusable UI components. This course will delve deeply into the core concepts of React: &#8216;components&#8217; and &#8216;state&#8217;. 1. What is a component? A component is the basic building block of React, representing small pieces that make up &hellip; \ub354 \ubcf4\uae30 \"React Course: Components and State\"","og_url":"https:\/\/atmokpo.com\/w\/32955\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:12:44+00:00","article_modified_time":"2024-11-01T11:21:19+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\/32955\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32955\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"React Course: Components and State","datePublished":"2024-11-01T09:12:44+00:00","dateModified":"2024-11-01T11:21:19+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32955\/"},"wordCount":645,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["React basics course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32955\/","url":"https:\/\/atmokpo.com\/w\/32955\/","name":"React Course: Components and State - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:12:44+00:00","dateModified":"2024-11-01T11:21:19+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32955\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32955\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32955\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"React Course: Components and State"}]},{"@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\/32955","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=32955"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32955\/revisions"}],"predecessor-version":[{"id":32956,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32955\/revisions\/32956"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}