<?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>Forem: PavloReutskiy</title>
    <description>The latest articles on Forem by PavloReutskiy (@pavloreutskiy).</description>
    <link>https://forem.com/pavloreutskiy</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%2F1116843%2F2e0ae720-19c8-4648-9220-1b25c44ebea8.jpeg</url>
      <title>Forem: PavloReutskiy</title>
      <link>https://forem.com/pavloreutskiy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/pavloreutskiy"/>
    <language>en</language>
    <item>
      <title>A Deep Dive into React Portals: From Theory to Practice</title>
      <dc:creator>PavloReutskiy</dc:creator>
      <pubDate>Sun, 24 Mar 2024 15:08:20 +0000</pubDate>
      <link>https://forem.com/pavloreutskiy/a-deep-dive-into-react-portals-from-theory-to-practice-nhm</link>
      <guid>https://forem.com/pavloreutskiy/a-deep-dive-into-react-portals-from-theory-to-practice-nhm</guid>
      <description>&lt;p&gt;I remember when I first attempted to create a modal window, I turned to YouTube looking for detailed explanations or tutorials. I was aware of &lt;em&gt;React Portal's&lt;/em&gt; existence but had no experience using it. To my surprise, I found that most recommendations were about creating a simple modal component without utilizing the portal. This approach can lead to unexpected errors since such a component might be reused across different parts of an application. But let's start from the beginning.&lt;/p&gt;

&lt;p&gt;In this article, I will explain in detail the advantages of &lt;em&gt;React Portal&lt;/em&gt;, the problems it can solve, and why using it is the perfect solution for creating modal windows, tooltips, and other elements that need to appear on top of the page's other content.&lt;/p&gt;

&lt;h2&gt;
  
  
  ✔️ Modal
&lt;/h2&gt;

&lt;p&gt;Firstly, let me clarify what I mean when I talk about a standard modal component without a portal.&lt;/p&gt;

&lt;p&gt;It's a standard component that accepts &lt;code&gt;children&lt;/code&gt; as props and renders content passed from the parent component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxuwsh1wel2u5eve8463g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxuwsh1wel2u5eve8463g.png" alt="Image description" width="800" height="509"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The parent component manages the state of the modal window using the &lt;code&gt;useState&lt;/code&gt; hook. This state changes when buttons are clicked in both the parent component and the modal itself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mr7hlglah0s4oy2xn0e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3mr7hlglah0s4oy2xn0e.png" alt="Image description" width="800" height="697"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To ensure the modal window appears over the main content of the page, we add the necessary CSS styles.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpdu15r8dgve47ct2wrwb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpdu15r8dgve47ct2wrwb.png" alt="Image description" width="800" height="692"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is a very simple example of implementing a modal window, and the logic behind its creation doesn’t change much when using &lt;em&gt;React Portal&lt;/em&gt;. However, there's an important detail - the &lt;strong&gt;stacking context&lt;/strong&gt;, which we'll discuss next.&lt;/p&gt;

&lt;h2&gt;
  
  
  ✔️ Stacking context
&lt;/h2&gt;

&lt;p&gt;Almost every front-end developer has encountered a scenario where, despite a high &lt;code&gt;z-index&lt;/code&gt;, an element doesn't appear on top of all others. The cause of this situation is the stacking context. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Finaklryrefo43itnl4aj.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Finaklryrefo43itnl4aj.jpeg" alt="Image description" width="564" height="564"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;According to documentation, the stacking context is a concept of three-dimensional arrangement of HTML elements along the Z-axis relative to the viewer. By default, all elements on the page are placed in the order they appear in the HTML. However, under certain conditions, such as absolute positioning with a &lt;code&gt;z-index&lt;/code&gt; different from &lt;code&gt;auto&lt;/code&gt;, &lt;code&gt;opacity&lt;/code&gt; less than &lt;code&gt;1&lt;/code&gt;, among others (more details &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_positioned_layout/Understanding_z-index/Stacking_context"&gt;here&lt;/a&gt;), a new stacking context can be formed.&lt;/p&gt;

&lt;p&gt;Consider an example where several absolutely positioned blocks of the same size but different colors are created (&lt;a href="https://codepen.io/Pavlo-Reutskyi/pen/wvZdzbo"&gt;codepen&lt;/a&gt;). Next, we will explore several scenarios by changing the &lt;code&gt;z-index&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Which color will be on top in the following example?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F78j2a0ghqywcwqfrr1gk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F78j2a0ghqywcwqfrr1gk.png" alt="Image description" width="800" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The correct answer is &lt;strong&gt;yellow&lt;/strong&gt;. This is because at the &lt;code&gt;body&lt;/code&gt; level, there exists a basic stacking context that determines the display order of the &lt;code&gt;green&lt;/code&gt; and &lt;code&gt;blue&lt;/code&gt; elements. Since &lt;code&gt;blue&lt;/code&gt; has a higher &lt;code&gt;z-index&lt;/code&gt;, it and all its child elements will be placed above all others at the same level. &lt;/p&gt;

&lt;p&gt;Let's consider another example with a slight change in conditions. Which element will now be higher than the others?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmkk5hsfzwk3lk0js8viu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmkk5hsfzwk3lk0js8viu.png" alt="Image description" width="800" height="531"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The correct answer is &lt;strong&gt;orange&lt;/strong&gt;. The &lt;code&gt;blue&lt;/code&gt; element, within its stacking context, "defeats" neighboring elements while simultaneously creating a new stacking context. Its child elements (&lt;code&gt;yellow&lt;/code&gt; and &lt;code&gt;orange&lt;/code&gt;) will compete with each other within this context. The &lt;code&gt;z-index&lt;/code&gt; of the parent element, though higher, is not considered.&lt;/p&gt;

&lt;p&gt;This can be likened to a wardrobe with shelves stacked on top of each other, which can be rearranged at will. Each shelf contains clothes arranged one on top of the other. The bottom shelf may contain many items, but they will always be lower than even a single item on the top shelf. The shelves and the clothes on each shelf represent stacking contexts.&lt;/p&gt;

&lt;p&gt;And here's the final example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7l2uw7ho970qu5lsvcpp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7l2uw7ho970qu5lsvcpp.png" alt="Image description" width="800" height="878"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You might have guessed that the behavior of the modal window will not be as expected, as the &lt;code&gt;blue&lt;/code&gt; element, with its higher &lt;code&gt;z-index&lt;/code&gt;, will not allow it to unfold over all other elements on the page.&lt;/p&gt;

&lt;p&gt;In this simple example, the problem is evident. However, when dealing with a large page composed of many components, such a scenario can easily go unnoticed. Moreover, the &lt;code&gt;Modal&lt;/code&gt; component might work perfectly on some pages and encounter problems on others. Often, such issues are challenging to diagnose.&lt;/p&gt;

&lt;p&gt;But there is a solution – &lt;strong&gt;React Portal&lt;/strong&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  ✔️ React Portal
&lt;/h2&gt;

&lt;p&gt;Portals offer a unique capability in React: they allow us to render child elements into different parts of the DOM that exist outside the parent component's DOM hierarchy, thus making them independent of the stacking context. This is achieved using the &lt;code&gt;createPortal()&lt;/code&gt; method, which can take up to three arguments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;children&lt;/code&gt; - can be anything from JSX, components, to even simple strings or numbers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;domNode&lt;/code&gt; - this is the DOM node where the portal will be placed. Typically, it's an element created outside the root element to which the React app is directly attached.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;key&lt;/code&gt; - an optional parameter that can be a number or a string, serving as a unique identifier for the portal.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's modify our &lt;code&gt;Modal&lt;/code&gt; component to use React Portal.&lt;/p&gt;

&lt;p&gt;First, we add an element with the identifier &lt;code&gt;portal&lt;/code&gt;, where our portal will be added. The identifier can be any value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsit082w3hs7eoc6cro6d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsit082w3hs7eoc6cro6d.png" alt="Image description" width="800" height="623"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, in the &lt;code&gt;Modal&lt;/code&gt; component itself, we import &lt;code&gt;createPortal&lt;/code&gt; from the &lt;code&gt;react-dom&lt;/code&gt; library. Instead of returning JSX as we did before, we use &lt;code&gt;createPortal&lt;/code&gt;. The first parameter is what we want to add, and the second parameter is where we want to add it. In our case, it's the element with the identifier &lt;code&gt;portal&lt;/code&gt;, adjacent to &lt;code&gt;root&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwxg85yht503qd6evxfgw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwxg85yht503qd6evxfgw.png" alt="Image description" width="800" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And just like that, it's done! We can now use the &lt;code&gt;Modal&lt;/code&gt; component as before, but with the confidence that this component and the &lt;code&gt;children&lt;/code&gt; passed into it will always be on top of the page because it is located outside its bounds.&lt;/p&gt;

&lt;p&gt;In general, we can highlight two key advantages and features of React Portal.&lt;/p&gt;

&lt;p&gt;🔹 As we've already discovered, the first advantage is that the portal allows us to bypass the limitations imposed by the parent element, especially through the stacking context or, for example, when using &lt;code&gt;overflow: hidden&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;🔹 The second, and perhaps most interesting feature, is that although the portal changes the physical location of the element in the DOM, it does not change its behavior relative to the React hierarchy. This means our &lt;code&gt;Modal&lt;/code&gt; component continues to behave as if it is a regular child element of the component rendering it. This includes the ability to use Context and the rules of event propagation.&lt;/p&gt;

&lt;p&gt;These features, presumably, explain the name. The component, using a portal, seemingly teleports to the required place in the React app but behaves as if it has always been there.&lt;/p&gt;

&lt;h2&gt;
  
  
  ✔️ React Portal in Next.js and Server Components
&lt;/h2&gt;

&lt;p&gt;When using portals in &lt;strong&gt;Next.js&lt;/strong&gt;, a few considerations need to be taken into account.&lt;/p&gt;

&lt;p&gt;Since a portal manipulates the DOM directly, it can only be a &lt;strong&gt;client component&lt;/strong&gt;, which requires the &lt;code&gt;"use client"&lt;/code&gt; directive. Additionally, before adding a portal, you need to ensure the DOM has been fully loaded. This can be achieved by using state and the &lt;code&gt;useEffect&lt;/code&gt; hook, which runs at the end, after the component has been rendered.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8vg1ieexe3tiw455uue3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8vg1ieexe3tiw455uue3.png" alt="Image description" width="800" height="771"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Although the portal will always be a client component, the &lt;code&gt;children&lt;/code&gt; passed into it can still be &lt;strong&gt;server component&lt;/strong&gt;, but only if the parent component is also server-side.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxicbbarvwkeo0rujg2f2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxicbbarvwkeo0rujg2f2.png" alt="Image description" width="800" height="553"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  ✔️ Summary
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;React Portal&lt;/strong&gt; is a cool and powerful tool. It may not be necessary for every modal implementation, especially in smaller projects, but there are many scenarios where it can significantly help and simplify a developer's life.&lt;/p&gt;

&lt;p&gt;I hope this article has been helpful and demonstrated when and how to use React Portal effectively. It not only solves specific challenges efficiently but also opens up new possibilities for optimization and enhancing the user experience in your applications.&lt;/p&gt;

&lt;p&gt;Thank you for your time and wish you success on your exciting journey of web development with React!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Magic of Closures in JavaScript for Beginners</title>
      <dc:creator>PavloReutskiy</dc:creator>
      <pubDate>Sat, 12 Aug 2023 13:52:44 +0000</pubDate>
      <link>https://forem.com/pavloreutskiy/the-magic-of-closures-in-javascript-for-beginners-3opp</link>
      <guid>https://forem.com/pavloreutskiy/the-magic-of-closures-in-javascript-for-beginners-3opp</guid>
      <description>&lt;p&gt;Closures are a very important part of JavaScript that every developer should know well. But, this topic often becomes a real challenge for beginners and can be difficult to understand. In this article, I want to explain closures in JavaScript in a very simple way.&lt;/p&gt;

&lt;p&gt;To understand closures, you need to know three main concepts:&lt;/p&gt;

&lt;p&gt;🔹 Scope&lt;/p&gt;

&lt;p&gt;🔹 Lexical environment&lt;/p&gt;

&lt;p&gt;🔹 The closure itself&lt;/p&gt;

&lt;h2&gt;
  
  
  ✔️ Scope
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Scope&lt;/em&gt;&lt;/strong&gt; is a part of code where you can use a variable, function, or object. In other words, it's the part of code where this item is "seen". &lt;/p&gt;

&lt;p&gt;Most of the time (let's pretend the &lt;code&gt;var&lt;/code&gt; keyword doesn't exist), the scope is the code block inside curly brackets - &lt;code&gt;{...}&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This isn't exactly right, but to keep it simple, we can say every function, and also code blocks like &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;for&lt;/code&gt;, &lt;code&gt;while&lt;/code&gt;, etc. (when using &lt;code&gt;let&lt;/code&gt; or &lt;code&gt;const&lt;/code&gt; variables), makes a new local scope.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;In JavaScript, there are 2 main scopes:&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Global Scope&lt;/strong&gt; - variables declared outside of functions or code blocks can be used anywhere in the code. They are global.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Local Scope&lt;/strong&gt; - as mentioned, variables inside a function (&lt;em&gt;Function Scope&lt;/em&gt;) or code block (&lt;em&gt;Block Scope&lt;/em&gt;) can only be used inside that block.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's look at an example.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8uyifjkhp597m21t9t5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn8uyifjkhp597m21t9t5.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see, the &lt;code&gt;playerHealth&lt;/code&gt; variable is in the global scope, so we can use it both globally and inside a function. But the &lt;code&gt;damageFromMonster&lt;/code&gt; variable was declared inside a function, which means it's within a local scope. That's why, when we try to access it outside the function, we get an error.&lt;/p&gt;

&lt;p&gt;This example also shows that different scopes can interact with each other. Furthermore, local scopes have access to variables from the outer scope. And this leads us to the concept of "lexical environment".&lt;/p&gt;

&lt;h2&gt;
  
  
  ✔️ Lexical environment
&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;&lt;strong&gt;Lexical environment&lt;/strong&gt;&lt;/em&gt; is a data structure that stores information about variables, functions, and other objects in the current execution context. It's an invisible object that exists for every block of code or function in JavaScript. It only exists in the computer's memory during code execution, so it can't be physically seen.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This object consists of two parts:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;an object with variables in the current scope,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;a reference to the parent lexical environment.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's go back to the previous example. We have 2 lexical environments.&lt;/p&gt;

&lt;p&gt;🌍 &lt;em&gt;Global&lt;/em&gt;, which doesn't have a reference to a parent lexical environment because one doesn't exist.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2f2epbc99rnn7zuuux8j.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2f2epbc99rnn7zuuux8j.png" alt="example-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;🔐 &lt;em&gt;Local&lt;/em&gt;, which refers to the global lexical environment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ss3vi6427ugyuwf00dm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8ss3vi6427ugyuwf00dm.png" alt="example-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;❗️I want to emphasize that the examples above are not real code, but just an attempt to visually imagine how the lexical environment object might look.&lt;/p&gt;

&lt;p&gt;It's also important to note that the local lexical environment for the &lt;code&gt;monsterAttack&lt;/code&gt; function is created not when we declare the function, but when we call it (&lt;code&gt;monsterAttack()&lt;/code&gt;). This is a crucial point for understanding closures, which we will discuss next.&lt;/p&gt;

&lt;h2&gt;
  
  
  ✔️ Closure
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Closures&lt;/em&gt;&lt;/strong&gt; in JavaScript - it is a feature where a function "remembers" its lexical environment in which it was created. This means that a function can access external variables even after the outer code has finished its execution.&lt;/p&gt;

&lt;p&gt;In other words, when calling a function, its lexical environment is established, storing the current values of variables at the time of the call, as well as referencing its outer environment. This allows the function to "see" the variables that were accessible to it when it was declared.&lt;/p&gt;

&lt;p&gt;Indeed, this concept might seem a bit confusing at first. However, in practice, everything becomes much clearer. &lt;/p&gt;

&lt;p&gt;Let's look at an example.&lt;/p&gt;

&lt;p&gt;Imagine we're inside a video game. In game's editor, we create a character. Let's choose a wizard to whom we assign a name (&lt;code&gt;name&lt;/code&gt;). This wizard will immediately have a basic spell (&lt;code&gt;castSpell&lt;/code&gt;), as well as 100 mana (&lt;code&gt;mana&lt;/code&gt;) for its use. The creation of such a character can be represented by the following function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fofo4b416yjj451y3fmbn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fofo4b416yjj451y3fmbn.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we click the "Create" button, a character is created, and it might look something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd4by3kyoajg53kkmx3sv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd4by3kyoajg53kkmx3sv.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point, a lexical environment for the &lt;code&gt;createWizard&lt;/code&gt; function will be created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0kmht05g7y0fatj1pjga.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0kmht05g7y0fatj1pjga.png" alt="example-5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When this function completes execution, its lexical environment disappears, but some parts (such as the &lt;code&gt;mana&lt;/code&gt; and &lt;code&gt;name&lt;/code&gt; variables) are "remembered" due to the &lt;strong&gt;closure&lt;/strong&gt; of the &lt;code&gt;castSpell&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;After this, we execute our first attack using our spell.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp5fjeuacddviidy0x66v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fp5fjeuacddviidy0x66v.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point, another lexical environment will be created, now for the &lt;code&gt;castSpell&lt;/code&gt; function (which is stored in the &lt;code&gt;lightWizard&lt;/code&gt; variable). This environment will exist within the lexical environment created with the &lt;code&gt;lightWizard&lt;/code&gt; character, and therefore it will reference it and its variables (&lt;code&gt;name&lt;/code&gt; and &lt;code&gt;mana&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftfug37czw7m46t6xyeta.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftfug37czw7m46t6xyeta.png" alt="example-7"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the next attack, the mana will be reduced. This is because a new lexical environment for &lt;code&gt;lightWizard&lt;/code&gt; is created during the new spell cast (&lt;code&gt;castSpell&lt;/code&gt;). This environment will include the current mana value, which is 80. After the subsequent attack, only 70 will remain, and so on.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft8j372a7ddodjv7uuxyv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft8j372a7ddodjv7uuxyv.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4mq2pagfgfkecuquczpm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4mq2pagfgfkecuquczpm.png" alt="example-9"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, a new player appears in the game. He also uses the editor and creates a new wizard.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft6yhnsqb2ea5ecek0fro.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft6yhnsqb2ea5ecek0fro.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For this character, a new lexical environment is created, which differs from the previous one only in name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fulvc2s38dug2b46ieb2q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fulvc2s38dug2b46ieb2q.png" alt="example-11"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, a battle between the two wizards begins. Voldemort attacks Harry with a full 100 mana.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0t108l3lumjw8jjdg1r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw0t108l3lumjw8jjdg1r.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Harry strikes back, but he has less power since he has already used some of his mana.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz5qdwy1mq1hddiahn87w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz5qdwy1mq1hddiahn87w.png" alt="example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you've probably realized by now, a new lexical environment is created with each attack, storing the current data, including the remaining &lt;code&gt;mana&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Even though both wizards were created using the same &lt;code&gt;createWizard&lt;/code&gt; function, their execution contexts are different. Each one has its own separate variables. So, when creating a new wizard with &lt;code&gt;createWizard()&lt;/code&gt;, you always start with 100 mana.&lt;/p&gt;

&lt;p&gt;This example is quite basic, but it illustrates the core concept. I chose this approach to avoid complex explanations and to focus on the main idea of this significant and interesting topic. &lt;/p&gt;

&lt;p&gt;I hope this article sparks your interest and encourages you to learn more about closures.&lt;/p&gt;

</description>
      <category>closure</category>
      <category>beginners</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Todo App: Why is it a Good Choice for a Pet Project?</title>
      <dc:creator>PavloReutskiy</dc:creator>
      <pubDate>Mon, 10 Jul 2023 15:38:12 +0000</pubDate>
      <link>https://forem.com/pavloreutskiy/todo-app-why-is-it-a-good-choice-for-a-pet-project-d86</link>
      <guid>https://forem.com/pavloreutskiy/todo-app-why-is-it-a-good-choice-for-a-pet-project-d86</guid>
      <description>&lt;p&gt;Have you ever pondered whether it's worth it for newbie programmers to invest their time in creating yet another Todo App as a starter project?&lt;/p&gt;

&lt;p&gt;The Todo App has become a classic, often seen as a clichéd and overused concept for initial projects. But, what if we reconsider and view it from a fresh perspective? &lt;/p&gt;

&lt;p&gt;In this piece, I'm keen on sharing my insights and experiences, aiming to benefit developers who are setting out on their programming journey, just like I did.&lt;/p&gt;

&lt;h3&gt;
  
  
  🐝 Todo App: A Simple Task that Demands Comprehensive Knowledge
&lt;/h3&gt;

&lt;p&gt;A Todo App is a typical task manager.  You add tasks, mark them as completed, and delete them. It seems pretty straightforward at first. However, I strongly believe that it's an excellent exercise for beginners, and I'm going to try to convince you.&lt;/p&gt;

&lt;p&gt;Despite being a small project, it requires a vast range of knowledge. It's an opportunity to put into practice all the tools you've learned so far. &lt;/p&gt;

&lt;p&gt;For instance, while working on a React task, I had substantial hands-on experience with &lt;em&gt;hooks&lt;/em&gt; and understood the advantages of using &lt;em&gt;Context&lt;/em&gt; and general state management tools. Moreover, I utilized tools like &lt;em&gt;React.memo&lt;/em&gt;, &lt;em&gt;useMemo&lt;/em&gt;, and &lt;em&gt;useCallback&lt;/em&gt; to optimize the application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nUZSAJGK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2tpqkxmbvqwfbia8og9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nUZSAJGK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2tpqkxmbvqwfbia8og9.png" alt="Image description" width="649" height="531"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  👀 Todo App: Pay Attention to Project Structure
&lt;/h3&gt;

&lt;p&gt;Although the project may seem small, undertaking it makes you appreciate the significance of structuring your application properly, dividing logic into separate components and files, and maintaining code readability.&lt;/p&gt;

&lt;h3&gt;
  
  
  🎓 Todo App: A Chance to Familiarize Yourself with Additional Libraries
&lt;/h3&gt;

&lt;p&gt;If you're considering building a Todo App, I'd suggest deciding in advance the libraries you'd like to experiment with. For example, I used the &lt;a href="https://www.i18next.com/"&gt;i18next&lt;/a&gt; library to understand how to create multilingual applications and the &lt;a href="https://reactcommunity.org/react-transition-group/"&gt;React Transition Group&lt;/a&gt; to make the application more engaging and fluid. &lt;/p&gt;

&lt;p&gt;I assure you, a Todo App can be more than a simple starter project - it can be a playground for enhancing your skills.&lt;/p&gt;

&lt;h3&gt;
  
  
  🖥 Todo App: Not Just a Front-end Project
&lt;/h3&gt;

&lt;p&gt;You could develop the Todo App as a front-end-only project, handling the task list locally. But you could also take a step further and create a complete full-stack project. While working on this project, I used a student API provided by my school (&lt;em&gt;Mate academy&lt;/em&gt;🚀), which gave me practical experience working with APIs on the client side. However, I intend to return to the project and create my own &lt;em&gt;RESTful API&lt;/em&gt; using &lt;em&gt;Node.js&lt;/em&gt;. This will be a practice that, while simple, will be incredibly useful.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Dnfm5XGr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4lqx6jjgwdg4zm4y6o4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Dnfm5XGr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4lqx6jjgwdg4zm4y6o4.png" alt="Image description" width="800" height="536"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you wish, you can take it even further and add user authentication functionality so that the application can be used by multiple users. This will surely be a beneficial experience for your future career.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion.
&lt;/h3&gt;

&lt;p&gt;A Todo App is an excellent method to bring your first extensive project to life, applying your existing knowledge while also learning and exploring new tools. Of course, more intricate projects would make your portfolio stand out, but remember that your initial project should be achievable and enjoyable, both in process and outcome. &lt;/p&gt;

&lt;p&gt;For me, the Todo App was the gateway to experiencing programming and developing a lifelong passion for it.&lt;/p&gt;

&lt;p&gt;🙏 I appreciate your time spent reading this article. Since it's my first attempt at writing, your feedback and insights are invaluable to me. &lt;/p&gt;

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