{"id":32903,"date":"2024-11-01T09:12:22","date_gmt":"2024-11-01T09:12:22","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=32903"},"modified":"2024-11-01T11:21:32","modified_gmt":"2024-11-01T11:21:32","slug":"react-course-handling-events","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/32903\/","title":{"rendered":"React Course: Handling Events"},"content":{"rendered":"<p>React is a very useful library for building user interfaces. Event handling is essential for processing user input in React applications. In this course, we will delve deeply into how to handle events in React.<\/p>\n<h2>1. What are Events?<\/h2>\n<p>Events are specific actions or state changes that occur in the browser. For example, an event occurs when a user clicks a button or moves the mouse. React provides a way to easily handle these events, allowing you to change the application&#8217;s state through user interaction.<\/p>\n<h2>2. React&#8217;s Event System<\/h2>\n<p>React uses its own event system, which operates by using a virtual DOM instead of directly modifying the DOM to perform optimized updates. React events are similar to standard DOM events, but there are some differences.<\/p>\n<h3>2.1 SyntheticEvent<\/h3>\n<p>React creates a SyntheticEvent object for all events. This object wraps the browser&#8217;s native events to provide a standardized API. For example, SyntheticEvent provides the <code>event.preventDefault()<\/code> and <code>event.stopPropagation()<\/code> methods.<\/p>\n<h3>2.2 Registering Event Handlers<\/h3>\n<p>In React, event handlers can be registered through JSX attributes. For example, you can handle events using attributes like <code>onClick<\/code>, <code>onChange<\/code>, and <code>onSubmit<\/code>.<\/p>\n<pre><code>\nfunction MyButton() {\n    function handleClick() {\n        alert('The button has been clicked!');\n    }\n\n    return (\n        &lt;button onClick={handleClick}&gt;Click here&lt;\/button&gt;\n    );\n}\n<\/code><\/pre>\n<h2>3. Basics of Event Handling<\/h2>\n<p>The basic way to handle events in React is to define a function and pass that function to an event property in JSX, as described above. Here we will look at a few common event types.<\/p>\n<h3>3.1 Mouse Events<\/h3>\n<p>Mouse events play an important role in handling interactions with the user interface. Here are examples of mouse events:<\/p>\n<pre><code>\nfunction MouseEventExample() {\n    function handleMouseOver() {\n        console.log('The mouse is over the element!');\n    }\n\n    return (\n        &lt;div onMouseOver={handleMouseOver}&gt;Mouse over test&lt;\/div&gt;\n    );\n}\n<\/code><\/pre>\n<h3>3.2 Keyboard Events<\/h3>\n<p>Keyboard events occur when a user inputs through the keyboard. Here is a basic example of key input events:<\/p>\n<pre><code>\nfunction KeyEventExample() {\n    function handleKeyPress(event) {\n        console.log('Key pressed:', event.key);\n    }\n\n    return (\n        &lt;input type=\"text\" onKeyPress={handleKeyPress} \/&gt;\n    );\n}\n<\/code><\/pre>\n<h3>3.3 Form Events<\/h3>\n<p>When using forms in React, it is important to utilize form events to manage input values. Here is an example of handling the form submit event:<\/p>\n<pre><code>\nfunction FormExample() {\n    function handleSubmit(event) {\n        event.preventDefault(); \/\/ Prevent default action\n        console.log('Form has been submitted!');\n    }\n\n    return (\n        &lt;form onSubmit={handleSubmit}&gt;\n            &lt;input type=\"text\" \/&gt;\n            &lt;button type=\"submit\"&gt;Submit&lt;\/button&gt;\n        &lt;\/form&gt;\n    );\n}\n<\/code><\/pre>\n<h2>4. Event Propagation and Binding<\/h2>\n<p>There are various ways to define event handlers in React, and they may function differently in class components and function components. This section will cover how to bind event handlers in class components.<\/p>\n<h3>4.1 Binding in Class Components<\/h3>\n<p>In class components, methods are not automatically bound to the instance of the class, so you need to bind the event handler methods. Here is an example using a class:<\/p>\n<pre><code>\nclass MyComponent extends React.Component {\n    constructor(props) {\n        super(props);\n        this.handleClick = this.handleClick.bind(this);\n    }\n\n    handleClick() {\n        alert('Clicked!');\n    }\n\n    render() {\n        return (\n            &lt;button onClick={this.handleClick}&gt;Click here&lt;\/button&gt;\n        );\n    }\n}\n<\/code><\/pre>\n<h3>4.2 Binding with Arrow Functions<\/h3>\n<p>In class components, using arrow functions to define methods automatically provides a binding effect:<\/p>\n<pre><code>\nclass MyComponent extends React.Component {\n    handleClick = () =&gt; {\n        alert('Clicked!');\n    }\n\n    render() {\n        return (\n            &lt;button onClick={this.handleClick}&gt;Click here&lt;\/button&gt;\n        );\n    }\n}\n<\/code><\/pre>\n<h2>5. Event Propagation and Capturing<\/h2>\n<p>When an event occurs on a web page, event propagation occurs, which can be divided into two stages: capturing and bubbling. React supports both of these stages.<\/p>\n<h3>5.1 Capturing<\/h3>\n<p>Event capturing is the stage where the event starts at the top-level element and proceeds to the element where the event occurred. Event handlers can be processed during the capturing stage using properties like <code>onClickCapture<\/code>.<\/p>\n<pre><code>\nfunction CapturingExample() {\n    function handleClickCapture() {\n        console.log('Clicked during the capturing phase!');\n    }\n\n    return (\n        &lt;div onClickCapture={handleClickCapture}&gt;\n            &lt;button&gt; Please click &lt;\/button&gt;\n        &lt;\/div&gt;\n    );\n}\n<\/code><\/pre>\n<h3>5.2 Bubbling<\/h3>\n<p>Bubbling is the stage where the event propagates from the element where it occurred to the top-level element, and it is typically handled in standard event handlers like <code>onClick<\/code>. React&#8217;s event system can handle both of these.<\/p>\n<h2>6. Handling Multiple Events<\/h2>\n<p>In React applications, you often need to handle various events simultaneously. In this case, you can define multiple event handlers within a single component to manage each event:<\/p>\n<pre><code>\nfunction MultiEventExample() {\n    function handleClick() {\n        console.log('Button clicked');\n    }\n\n    function handleMouseOver() {\n        console.log('Mouse over');\n    }\n\n    return (\n        &lt;button onClick={handleClick} onMouseOver={handleMouseOver}&gt;Button&lt;\/button&gt;\n    );\n}\n<\/code><\/pre>\n<h2>7. Event Handling with Custom Hooks<\/h2>\n<p>React allows you to create reusable logic using hooks. Implementing event handlers as custom hooks can improve the readability and maintainability of your code.<\/p>\n<pre><code>\nimport { useState } from 'react';\n\nfunction useEventHandler() {\n    const [count, setCount] = useState(0);\n\n    function increment() {\n        setCount(count + 1);\n    }\n\n    return [count, increment];\n}\n\nfunction EventHandlerExample() {\n    const [count, increment] = useEventHandler();\n\n    return (\n        &lt;div&gt;\n            &lt;p&gt;Count: {count}&lt;\/p&gt;\n            &lt;button onClick={increment}&gt;Increase&lt;\/button&gt;\n        &lt;\/div&gt;\n    );\n}\n<\/code><\/pre>\n<h2>8. Performance Optimization<\/h2>\n<p>Since event handling can occur frequently in React, performance optimization is important. Here are some ways to enhance performance:<\/p>\n<h3>8.1 Using React.memo<\/h3>\n<p>You can use <code>React.memo<\/code> to memoize components in React. This helps prevent unnecessary re-rendering.<\/p>\n<pre><code>\nconst MyComponent = React.memo(function MyComponent({ onClick }) {\n    return &lt;button onClick={onClick}&gt;Click here&lt;\/button&gt;;\n});\n<\/code><\/pre>\n<h3>8.2 Using useCallback Hook<\/h3>\n<p>By using the <code>useCallback<\/code> hook to memoize event handlers, you can avoid creating new handlers when the component re-renders.<\/p>\n<pre><code>\nimport { useCallback } from 'react';\n\nfunction EventOptimizationExample() {\n    const handleClick = useCallback(() =&gt; {\n        console.log('Clicked!');\n    }, []);\n\n    return &lt;button onClick={handleClick}&gt;Click here&lt;\/button&gt;;\n}\n<\/code><\/pre>\n<h2>9. Conclusion<\/h2>\n<p>In this course, we have taken a detailed look at handling events in React. By understanding React&#8217;s event system, various event types, and efficient event handling methods, you can implement better user interactions. We hope you will continue to utilize React to develop high-quality applications.<\/p>\n<p>In the next course, we will cover state management and asynchronous processing in React. Let&#8217;s continue to explore the charm of React!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>React is a very useful library for building user interfaces. Event handling is essential for processing user input in React applications. In this course, we will delve deeply into how to handle events in React. 1. What are Events? Events are specific actions or state changes that occur in the browser. For example, an event &hellip; <a href=\"https:\/\/atmokpo.com\/w\/32903\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;React Course: Handling Events&#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-32903","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: Handling Events - \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\/32903\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"React Course: Handling Events - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"React is a very useful library for building user interfaces. Event handling is essential for processing user input in React applications. In this course, we will delve deeply into how to handle events in React. 1. What are Events? Events are specific actions or state changes that occur in the browser. For example, an event &hellip; \ub354 \ubcf4\uae30 &quot;React Course: Handling Events&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/32903\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:12:22+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:21:32+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=\"5\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/32903\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32903\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"React Course: Handling Events\",\"datePublished\":\"2024-11-01T09:12:22+00:00\",\"dateModified\":\"2024-11-01T11:21:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32903\/\"},\"wordCount\":672,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"React basics course\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/32903\/\",\"url\":\"https:\/\/atmokpo.com\/w\/32903\/\",\"name\":\"React Course: Handling Events - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:12:22+00:00\",\"dateModified\":\"2024-11-01T11:21:32+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/32903\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/32903\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/32903\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"React Course: Handling Events\"}]},{\"@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: Handling Events - \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\/32903\/","og_locale":"ko_KR","og_type":"article","og_title":"React Course: Handling Events - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"React is a very useful library for building user interfaces. Event handling is essential for processing user input in React applications. In this course, we will delve deeply into how to handle events in React. 1. What are Events? Events are specific actions or state changes that occur in the browser. For example, an event &hellip; \ub354 \ubcf4\uae30 \"React Course: Handling Events\"","og_url":"https:\/\/atmokpo.com\/w\/32903\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:12:22+00:00","article_modified_time":"2024-11-01T11:21:32+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":"5\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/32903\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/32903\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"React Course: Handling Events","datePublished":"2024-11-01T09:12:22+00:00","dateModified":"2024-11-01T11:21:32+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/32903\/"},"wordCount":672,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["React basics course"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/32903\/","url":"https:\/\/atmokpo.com\/w\/32903\/","name":"React Course: Handling Events - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:12:22+00:00","dateModified":"2024-11-01T11:21:32+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/32903\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/32903\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/32903\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"React Course: Handling Events"}]},{"@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\/32903","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=32903"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32903\/revisions"}],"predecessor-version":[{"id":32904,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/32903\/revisions\/32904"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=32903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=32903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=32903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}