<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: YogiYiorgos</title>
    <description>The latest articles on DEV Community by YogiYiorgos (@yogiyiorgos).</description>
    <link>https://dev.to/yogiyiorgos</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F830153%2F376590ce-a019-49ab-81be-6e34c392edf9.png</url>
      <title>DEV Community: YogiYiorgos</title>
      <link>https://dev.to/yogiyiorgos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yogiyiorgos"/>
    <language>en</language>
    <item>
      <title>Exploring the Inner Workings of the JavaScript Event Loop</title>
      <dc:creator>YogiYiorgos</dc:creator>
      <pubDate>Sat, 25 Mar 2023 13:05:30 +0000</pubDate>
      <link>https://dev.to/yogiyiorgos/exploring-the-inner-workings-of-the-javascript-event-loop-1j</link>
      <guid>https://dev.to/yogiyiorgos/exploring-the-inner-workings-of-the-javascript-event-loop-1j</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Asynchronous programming is an essential part of modern web development. Whether you're making network requests to APIs, handling user input in the browser, or performing complex calculations, it's important to understand how JavaScript handles asynchronous tasks. One key concept in JavaScript's approach to asynchronous programming is the event loop.&lt;/p&gt;

&lt;p&gt;The event loop is the mechanism that allows JavaScript to execute multiple tasks concurrently without blocking the main thread. In this blog post, we'll explore what the event loop is, how it works, and provide real-life examples of how it's used in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is the event loop?
&lt;/h2&gt;

&lt;p&gt;Specifically in Javascript, the event loop is a mechanism that allows the single threaded environment of the language to handle asynchronous operations. The event loop is a continuous process that runs in the background of JavaScript's execution environment, constantly monitoring a queue of tasks that need to be processed. When the main thread finishes executing a task, the event loop checks the task queue for any pending tasks and processes them in the order they were received. This allows JavaScript to execute multiple tasks concurrently, without blocking the main thread.&lt;/p&gt;

&lt;h2&gt;
  
  
  How does the event loop work?
&lt;/h2&gt;

&lt;p&gt;In Javascript, there is only one main thread of execution that processes code in a linear fashion, one line at a time. When an asynchronous operation is initiated, such as a network request, it is handed off to a separate thread of execution provided by the browser, such as the Web API thread, to be processed in the background. The main thread then continues executing the next line of code, without waiting for the asynchronous operation to complete.&lt;/p&gt;

&lt;p&gt;Once the asynchronous operation is completed, it is added to a queue of events that are managed by the event loop. The event loop constantly monitors this queue and, when the queue is not empty, it processes the events in a FIFO order.&lt;/p&gt;

&lt;p&gt;The event loop uses a two-phase cycle to manage the queue of events. In the first phase, the event loop processes all synchronous events in the queue, such as mouse clicks and button presses. These events are typically processed quickly and are not likely to block the main thread. In the second phase, the event loop processes asynchronous events, such as callbacks from network requests, that have been added to the queue.&lt;/p&gt;

&lt;p&gt;When the event loop processes an asynchronous event, it runs the associated callback function on the main thread. This can result in the callback being executed at any point in the program's execution, even if it is in the middle of processing another task. This is why callback functions must be designed to be non-blocking and lightweight to avoid blocking the main thread for too long.&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-life examples
&lt;/h2&gt;

&lt;p&gt;Let's look at some real-life examples of how the event loop is used in Javascript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1: Network request
&lt;/h3&gt;

&lt;p&gt;When we make a network request in Javascript, the request is handled asynchronously by the browser's networking stack. This means that the main thread can continue executing code while the request is being processed in the background. Once the response is received, the callback function is added to the task queue, and the event loop processes it when it's the next item in the queue.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Example 2: Timer
&lt;/h3&gt;

&lt;p&gt;When we use the &lt;code&gt;setTimeout&lt;/code&gt; function to schedule a timer in Javascript, the timer is handled asynchronously by the browser's timer API. This means that the main thread can continue executing code while the timer is counting down in the background. Once the timer expires, the callback function is added to the task queue, and the event loop processes it when it's the next item in the queue.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Starting&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Set a timer to execute after 2 seconds&lt;/span&gt;
&lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Timeout 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Set another timer to execute after 1 second&lt;/span&gt;
&lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;TImeout 2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Finishing&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When this code is executed, the following happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The first &lt;code&gt;console.log&lt;/code&gt; statement is executed, &lt;strong&gt;logging "Starting" to the console&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The first &lt;code&gt;setTimeout&lt;/code&gt; call is made, scheduling the first callback function to be executed after 2 seconds. However, since this is an asynchronous operation, the main thread continues executing the next line of code without waiting for the timer to expire.&lt;/li&gt;
&lt;li&gt;The second &lt;code&gt;setTimeout&lt;/code&gt; call is made, scheduling the second callback function to be executed after 1 second. Again, the main thread continues executing the next line of code without waiting for the timer to expire.&lt;/li&gt;
&lt;li&gt;The final &lt;code&gt;console.log&lt;/code&gt; statement is executed, &lt;strong&gt;logging "Finishing" to the console&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The event loop continues to run on the main thread, monitoring the queue of events.&lt;/li&gt;
&lt;li&gt;After 1 second has elapsed, the first timer expires and adds the associated callback function to the event queue.&lt;/li&gt;
&lt;li&gt;The event loop processes the first event in the queue, which is the callback function from the first &lt;code&gt;setTimeout&lt;/code&gt; call, &lt;strong&gt;logging "Timeout 2" to the console&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;After another second has elapsed, the second timer expires and adds the associated callback function to the event queue.&lt;/li&gt;
&lt;li&gt;The event loop processes the second event in the queue, which is the callback function from the second &lt;code&gt;setTimeout&lt;/code&gt; call, &lt;strong&gt;logging "Timeout 1" to the console&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The console prints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Starting
Finishing
Timeout 2
Timeout 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Beyond Props and State: Understanding and using Refs in React</title>
      <dc:creator>YogiYiorgos</dc:creator>
      <pubDate>Sun, 22 Jan 2023 11:11:16 +0000</pubDate>
      <link>https://dev.to/yogiyiorgos/beyond-props-and-state-understanding-and-using-refs-in-react-3741</link>
      <guid>https://dev.to/yogiyiorgos/beyond-props-and-state-understanding-and-using-refs-in-react-3741</guid>
      <description>&lt;p&gt;React is a powerful Javascript library for building user interfaces and one of the features it offers is the ability to create and use refs. In this blog post, we'll take a closer look at what refs are, when to use them, and how to create and use them in our React projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are refs in React?
&lt;/h2&gt;

&lt;p&gt;Refs are a way to access a component's underlying DOM (Document Object Model) node. They allow us to access the value of a form element, trigger a focus or media playback, and more. Refs can be created and used in both functional and class components.&lt;/p&gt;

&lt;h2&gt;
  
  
  When to use refs in React?
&lt;/h2&gt;

&lt;p&gt;Refs should be used sparingly and only when necessary. In general, it's recommended to use state and props to manage the state of our application. However, there are certain cases where refs offer a better solution:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Accessing the value of a form element&lt;/li&gt;
&lt;li&gt;Triggering a focus on a specific element&lt;/li&gt;
&lt;li&gt;Triggering media playback&lt;/li&gt;
&lt;li&gt;Integrating with third-party libraries that require direct access to the DOM
## How to create and use refs in React?
To create a ref, we can use the &lt;code&gt;useRef&lt;/code&gt; hook in functional components or the &lt;code&gt;createRef&lt;/code&gt; method in class components. Then, we can attach the ref to any JSX element using the &lt;code&gt;ref&lt;/code&gt; attribute.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inputRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handleSubmit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="nx"&gt;onSubmit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleSubmit&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;inputRef&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Submit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, &lt;code&gt;inputRef&lt;/code&gt; is a ref created using the &lt;code&gt;useRef&lt;/code&gt; hook. It is attached to the &lt;code&gt;input&lt;/code&gt; element using the &lt;code&gt;ref&lt;/code&gt; attribute. When the form is submitted, the &lt;code&gt;handleSubmit&lt;/code&gt; function is called and it logs the current value of the input field by accessing &lt;code&gt;inputRef.current.value&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can also use refs for other purposes such as triggering a focus on a specific element, or triggering media playback.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MyVideoPlayer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;videoRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;handlePlay&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;videoRef&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;play&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;video&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;videoRef&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;my-video.mp4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;video/mp4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/video&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handlePlay&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Play&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above shows how refs can be very useful, where &lt;code&gt;videoRef&lt;/code&gt; is a ref created using the &lt;code&gt;useRef&lt;/code&gt; hook and it is attached to the &lt;code&gt;video&lt;/code&gt; element using the &lt;code&gt;ref&lt;/code&gt; attribute. When the play button is clicked, the &lt;code&gt;handlePlay&lt;/code&gt; function is called and it triggers the play method on the video element by accessing &lt;code&gt;videoRef.current.play()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What makes refs so different?
&lt;/h2&gt;

&lt;p&gt;Now that we have covered the basics of hoe to create and use refs in a React application, let's delve deeper into some of the key features and considerations when utilizing refs.&lt;/p&gt;

&lt;p&gt;Refs have a few key differences form state and props that make them useful is certain situations. One important difference is that refs persist across re-renderes, unlike regular variables which reset in every render. This makes refs useful for storing information that needs to be accessed between renders, such as the current position of a video player or the scroll position of a container.&lt;/p&gt;

&lt;p&gt;Another difference is that changing a ref does not trigger a re-render, unlike state variables which, when they change, they trigger a re-render. This can be beneficial in situations where we need to update a value without causing a re-render, such as when updating the position of an element on a drag and drop interaction.&lt;/p&gt;

&lt;p&gt;Finally, refs are local to each copy of a component, unlike variables outside of the component which are shared. This means that each instance of a component can have its own ref, which can be useful in situations where multiple instances of a component need to maintain their own state independently. &lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In summary, refs in React offer a way to access the underlying DOM of a component, providing more control over our application. While it may be tempting to rely on refs for everything, it's important to remember that they should be used when other methods such as props and state are not sufficient. Used correctly, refs can be a powerful tool in our React arsenal.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>react</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
