{"id":32883,"date":"2024-11-01T09:12:13","date_gmt":"2024-11-01T09:12:13","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32883"},"modified":"2024-11-01T11:21:37","modified_gmt":"2024-11-01T11:21:37","slug":"react-course-variables-and-constants","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32883\/","title":{"rendered":"React Course: Variables and Constants"},"content":{"rendered":"<p>React is one of the most widely used JavaScript libraries for building modern web applications. To use React effectively, it is essential to understand the basic concepts of JavaScript, especially variables and constants. This article will deeply explore how to use variables and constants in React.<\/p>\n<h2>1. What is a variable?<\/h2>\n<p>A variable is a space to store data, which has the characteristic of being able to change its value as needed. In JavaScript, variables can be declared using three keywords: <code>var<\/code>, <code>let<\/code>, and <code>const<\/code>.<\/p>\n<h3>1.1 <code>var<\/code> keyword<\/h3>\n<p><code>var<\/code> is the oldest method of declaring variables and has function scope. This means that a variable declared with <code>var<\/code> is only valid within the function. It can be accessed globally at any time.<\/p>\n<pre><code>var x = 10;\nif (true) {\n    var x = 20; \/\/ Redefining x within the same scope\n}\nconsole.log(x); \/\/ Output: 20\n<\/code><\/pre>\n<h3>1.2 <code>let<\/code> keyword<\/h3>\n<p><code>let<\/code> has block scope, allowing variables to be declared that are only valid within a block. This allows for finer control over the variable&#8217;s scope.<\/p>\n<pre><code>let y = 10;\nif (true) {\n    let y = 20; \/\/ Redefining within a different block scope\n}\nconsole.log(y); \/\/ Output: 10\n<\/code><\/pre>\n<h3>1.3 <code>const<\/code> keyword<\/h3>\n<p><code>const<\/code> is used to declare constants, and the value cannot be changed after declaration. Similarly, <code>const<\/code> also has block scope.<\/p>\n<pre><code>const z = 30;\nif (true) {\n    const z = 40; \/\/ Redefining within a different block scope\n}\nconsole.log(z); \/\/ Output: 30\n<\/code><\/pre>\n<h2>2. Using variables in React<\/h2>\n<p>In React, variables and constants are used to compose the UI. They are mainly used to store the state of a component or to pass data via props.<\/p>\n<h3>2.1 Component State<\/h3>\n<p>The state of a React component is declared using the <code>useState<\/code> hook. This state can change when the component is rendered.<\/p>\n<pre><code>import React, { useState } from 'react';\n\nconst Counter = () =&gt; {\n    const [count, setCount] = useState(0); \/\/ Declare the count variable\n\n    return (\n        <div>\n            <p>Current Count: {count}<\/p>\n            <button onClick={() => setCount(count + 1)}>Increase<\/button>\n        <\/div>\n    );\n}\n<\/code><\/pre>\n<h3>2.2 Passing Data through Props<\/h3>\n<p>Props are used to pass data between components. Props are component properties, and can be used like variables.<\/p>\n<pre><code>const Greeting = ({ name }) =&gt; {\n    return <h1>Hello, {name}!<\/h1>;\n}\n\nconst App = () =&gt; {\n    return <Greeting name=\"React\"><\/Greeting>;\n}\n<\/code><\/pre>\n<h2>3. How to use constants<\/h2>\n<p>Constants represent unchanging values, helping to improve code readability and reduce bugs. In React, constants are mostly defined for styles or configuration values.<\/p>\n<pre><code>const API_URL = \"https:\/\/api.example.com\/data\";\n\nconst fetchData = async () =&gt; {\n    const response = await fetch(API_URL);\n    const data = await response.json();\n    console.log(data);\n}\n<\/code><\/pre>\n<h2>4. Example using variables and constants<\/h2>\n<p>Let&#8217;s discuss how to utilize variables and constants according to the life cycle. Below is a simple example of sending an API request and displaying data in React.<\/p>\n<pre><code>import React, { useState, useEffect } from 'react';\n\nconst DataFetcher = () =&gt; {\n    const [data, setData] = useState([]);\n    const API_URL = \"https:\/\/api.example.com\/data\";\n\n    useEffect(() =&gt; {\n        const fetchData = async () =&gt; {\n            const response = await fetch(API_URL);\n            const result = await response.json();\n            setData(result);\n        };\n\n        fetchData();\n    }, []);\n\n    return (\n        <ul>\n            {data.map(item =&gt; (\n                <li key={item.id}>{item.name}<\/li>\n            ))}\n        <\/ul>\n    );\n}\n<\/code><\/pre>\n<h2>5. Best Practices for Variables and Constants<\/h2>\n<ul>\n<li><strong>Name variables meaningfully:<\/strong> Variable names should clearly indicate their purpose.<\/li>\n<li><strong>Use uppercase for constants:<\/strong> Constants should be defined using uppercase letters and underscores (_).<\/li>\n<li><strong>Be aware of scope:<\/strong> Clearly understand and write the scope of variables and constants.<\/li>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>Correctly using variables and constants in React is crucial for writing stable and maintainable code. Based on the above content, try to effectively utilize variables and constants.<\/p>\n<p><strong>If you found this tutorial helpful, please leave a comment!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>React is one of the most widely used JavaScript libraries for building modern web applications. To use React effectively, it is essential to understand the basic concepts of JavaScript, especially variables and constants. This article will deeply explore how to use variables and constants in React. 1. What is a variable? A variable is a &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32883\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;React Course: Variables and Constants&#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-32883","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: Variables and Constants - \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\/32883\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Course: Variables and Constants - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"React is one of the most widely used JavaScript libraries for building modern web applications. To use React effectively, it is essential to understand the basic concepts of JavaScript, especially variables and constants. This article will deeply explore how to use variables and constants in React. 1. What is a variable? A variable is a &hellip; \ub354 \ubcf4\uae30 &quot;React Course: Variables and Constants&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32883\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:12:13+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:21:37+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\/32883\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32883\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"React Course: Variables and Constants\",\"datePublished\":\"2024-11-01T09:12:13+00:00\",\"dateModified\":\"2024-11-01T11:21:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32883\/\"},\"wordCount\":382,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"React basics course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32883\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32883\/\",\"name\":\"React Course: Variables and Constants - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:12:13+00:00\",\"dateModified\":\"2024-11-01T11:21:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32883\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32883\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32883\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Course: Variables and Constants\"}]},{\"@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: Variables and Constants - \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\/32883\/","og_locale":"ko_KR","og_type":"article","og_title":"React Course: Variables and Constants - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"React is one of the most widely used JavaScript libraries for building modern web applications. To use React effectively, it is essential to understand the basic concepts of JavaScript, especially variables and constants. This article will deeply explore how to use variables and constants in React. 1. What is a variable? A variable is a &hellip; \ub354 \ubcf4\uae30 \"React Course: Variables and Constants\"","og_url":"https:\/\/atmokpo.com\/w\/32883\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:12:13+00:00","article_modified_time":"2024-11-01T11:21:37+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\/32883\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32883\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"React Course: Variables and Constants","datePublished":"2024-11-01T09:12:13+00:00","dateModified":"2024-11-01T11:21:37+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32883\/"},"wordCount":382,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["React basics course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32883\/","url":"https:\/\/atmokpo.com\/w\/32883\/","name":"React Course: Variables and Constants - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:12:13+00:00","dateModified":"2024-11-01T11:21:37+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32883\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32883\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32883\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"React Course: Variables and Constants"}]},{"@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\/32883","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=32883"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32883\/revisions"}],"predecessor-version":[{"id":32884,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32883\/revisions\/32884"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}