{"id":32825,"date":"2024-11-01T09:11:49","date_gmt":"2024-11-01T09:11:49","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32825"},"modified":"2024-11-01T11:21:50","modified_gmt":"2024-11-01T11:21:50","slug":"react-course-in-depth-exploration-of-ref","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32825\/","title":{"rendered":"React Course: In-Depth Exploration of Ref"},"content":{"rendered":"<p>React is a widely used JavaScript library for modern web application development. React is based on components that make up the UI and maximizes user experience through client-side state management and efficient UI rendering. In this course, we will explore one of the various concepts of React, <strong>Ref<\/strong>, in detail.<\/p>\n<h2>1. What is Ref?<\/h2>\n<p>In React, <strong>Ref<\/strong> is a way to directly access DOM elements or React components. Ref is useful when there is a need to directly modify the DOM within React&#8217;s component model and lifecycle. This allows us to access DOM elements or perform tasks such as calling specific methods without using state.<\/p>\n<h2>2. The Need for Ref<\/h2>\n<p>In React, it is common to update the UI based on the component&#8217;s state. However, there are times when we need to directly access specific elements of the UI. In such situations, we use Ref. For example, Ref is helpful when we need to set focus to a specific input element or directly apply animation effects.<\/p>\n<h2>3. How to Use Ref<\/h2>\n<p>Here is how to create and use Ref in React.<\/p>\n<pre><code>import React, { useRef } from 'react';\n\nfunction MyComponent() {\n    const inputRef = useRef(null);\n\n    const focusInput = () =&gt; {\n        if (inputRef.current) {\n            inputRef.current.focus();\n        }\n    };\n\n    return (\n        <div>\n            <input ref=\"{inputRef}\" type=\"text\"\/>\n            <button onclick=\"{focusInput}\">Focus<\/button>\n        <\/div>\n    );\n}\n<\/code><\/pre>\n<p>In the example above, we used the <code>useRef<\/code> hook to create <code>inputRef<\/code> and added the ref attribute to the <code>input<\/code> element. The <code>focusInput<\/code> function is called when the button is clicked, setting the focus to the input element.<\/p>\n<h2>4. Use Cases for Ref<\/h2>\n<h3>4.1. Focus Management<\/h3>\n<p>We can implement a feature that moves the focus to a specific input field when the user presses the &#8220;Enter&#8221; key. In this case, we directly access the input field using Ref.<\/p>\n<h3>4.2. Animation and DOM Manipulation<\/h3>\n<p>When using an animation library with React, it may be necessary to make direct changes to the DOM elements. Using Ref allows us to easily apply fluid animation effects.<\/p>\n<h3>4.3. Integration with External Libraries<\/h3>\n<p>When there is a need to access DOM elements using specific external libraries, we can use Ref to select those elements and call methods from the library.<\/p>\n<h2>5. Difference Between Ref and State Management<\/h2>\n<p>While Ref and state appear to serve the same purpose, they are designed for different uses. State is a way to update the UI, while Ref is a way to access DOM elements. State operates alongside React&#8217;s lifecycle, whereas Ref is used for direct manipulation of the DOM.<\/p>\n<h2>6. Limitations of Ref<\/h2>\n<p>Ref has several limitations compared to state management. The main limitations are:<\/p>\n<ul>\n<li>Changes to Ref do not trigger re-rendering: Ref modifies the DOM directly, and changes to the element do not affect React&#8217;s state management system.<\/li>\n<li>Mainly used for direct DOM manipulation: Ref can lead to poor design patterns. It is recommended to manage state through components.<\/li>\n<\/ul>\n<h2>7. Example Using Ref<\/h2>\n<p>The example code below provides a text area where users can input text. When the user presses the &#8220;Enter&#8221; button, the focus is set to the text area.<\/p>\n<pre><code>import React, { useRef } from 'react';\n\nfunction TextInput() {\n    const textAreaRef = useRef(null);\n\n    const focusTextArea = () =&gt; {\n        if (textAreaRef.current) {\n            textAreaRef.current.focus();\n        }\n    };\n\n    return (\n        <div>\n            <textarea cols=\"50\" ref=\"{textAreaRef}\" rows=\"4\"><\/textarea>\n            <button onclick=\"{focusTextArea}\">Focus on Text Area<\/button>\n        <\/div>\n    );\n}\n<\/code><\/pre>\n<h2>8. Advanced Ref Usage<\/h2>\n<p>Refs can be used in deeper parts of React. For example, we will explain how to pass refs from a parent component to a child component using React&#8217;s <code>forwardRef<\/code>.<\/p>\n<pre><code>import React, { forwardRef, useRef } from 'react';\n\nconst FancyInput = forwardRef((props, ref) =&gt; (\n    <input style=\"border: 2px solid blue;\" ref=\"{ref}\" type=\"text\"\/>\n));\n\nfunction ParentComponent() {\n    const inputRef = useRef(null);\n\n    const focusInput = () =&gt; {\n        if (inputRef.current) {\n            inputRef.current.focus();\n        }\n    };\n\n    return (\n        <div>\n            <FancyInput ref=\"{inputRef}\"><\/FancyInput>\n            <button onclick=\"{focusInput}\">Focus<\/button>\n        <\/div>\n    );\n}\n<\/code><\/pre>\n<h2>9. Conclusion<\/h2>\n<p>Ref is a very useful feature in React that provides a way to directly access and manipulate DOM elements. We hope that this guide has helped you understand the concept, necessity, usage, and advanced features of Ref. Furthermore, we hope you can effectively utilize Ref to provide a better user experience and UI when developing web applications with React.<\/p>\n<h2>10. Additional Resources<\/h2>\n<ul>\n<li><a href=\"https:\/\/reactjs.org\/docs\/refs-and-the-dom.html\" target=\"_blank\" rel=\"noopener\">React Official Documentation &#8211; Refs and the DOM<\/a><\/li>\n<li><a href=\"https:\/\/react.dev\/learn\/refs-in-react\" target=\"_blank\" rel=\"noopener\">React Learn &#8211; Refs in React<\/a><\/li>\n<\/ul>\n<p>Please explore various resources to learn more about React and Ref. Thank you!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>React is a widely used JavaScript library for modern web application development. React is based on components that make up the UI and maximizes user experience through client-side state management and efficient UI rendering. In this course, we will explore one of the various concepts of React, Ref, in detail. 1. What is Ref? In &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32825\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;React Course: In-Depth Exploration of Ref&#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-32825","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: In-Depth Exploration of Ref - \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\/32825\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Course: In-Depth Exploration of Ref - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"React is a widely used JavaScript library for modern web application development. React is based on components that make up the UI and maximizes user experience through client-side state management and efficient UI rendering. In this course, we will explore one of the various concepts of React, Ref, in detail. 1. What is Ref? In &hellip; \ub354 \ubcf4\uae30 &quot;React Course: In-Depth Exploration of Ref&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32825\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:11:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:21:50+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\/32825\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32825\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"React Course: In-Depth Exploration of Ref\",\"datePublished\":\"2024-11-01T09:11:49+00:00\",\"dateModified\":\"2024-11-01T11:21:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32825\/\"},\"wordCount\":590,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"React basics course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32825\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32825\/\",\"name\":\"React Course: In-Depth Exploration of Ref - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:11:49+00:00\",\"dateModified\":\"2024-11-01T11:21:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32825\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32825\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32825\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Course: In-Depth Exploration of Ref\"}]},{\"@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: In-Depth Exploration of Ref - \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\/32825\/","og_locale":"ko_KR","og_type":"article","og_title":"React Course: In-Depth Exploration of Ref - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"React is a widely used JavaScript library for modern web application development. React is based on components that make up the UI and maximizes user experience through client-side state management and efficient UI rendering. In this course, we will explore one of the various concepts of React, Ref, in detail. 1. What is Ref? In &hellip; \ub354 \ubcf4\uae30 \"React Course: In-Depth Exploration of Ref\"","og_url":"https:\/\/atmokpo.com\/w\/32825\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:11:49+00:00","article_modified_time":"2024-11-01T11:21:50+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\/32825\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32825\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"React Course: In-Depth Exploration of Ref","datePublished":"2024-11-01T09:11:49+00:00","dateModified":"2024-11-01T11:21:50+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32825\/"},"wordCount":590,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["React basics course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32825\/","url":"https:\/\/atmokpo.com\/w\/32825\/","name":"React Course: In-Depth Exploration of Ref - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:11:49+00:00","dateModified":"2024-11-01T11:21:50+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32825\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32825\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32825\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"React Course: In-Depth Exploration of Ref"}]},{"@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\/32825","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=32825"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32825\/revisions"}],"predecessor-version":[{"id":32826,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32825\/revisions\/32826"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}