<?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: Lakshya Dhariwal</title>
    <description>The latest articles on Forem by Lakshya Dhariwal (@lakshya-dhariwal).</description>
    <link>https://forem.com/lakshya-dhariwal</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%2F489635%2F2aa081bc-bf20-43c9-be6f-169463cad32a.jpg</url>
      <title>Forem: Lakshya Dhariwal</title>
      <link>https://forem.com/lakshya-dhariwal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/lakshya-dhariwal"/>
    <language>en</language>
    <item>
      <title>Reusable Modal Component using React Portals</title>
      <dc:creator>Lakshya Dhariwal</dc:creator>
      <pubDate>Thu, 11 Apr 2024 16:43:59 +0000</pubDate>
      <link>https://forem.com/lakshya-dhariwal/reusable-modal-component-using-react-portals-17n6</link>
      <guid>https://forem.com/lakshya-dhariwal/reusable-modal-component-using-react-portals-17n6</guid>
      <description>&lt;h2&gt;
  
  
  How to create a reusable modal component from scratch
&lt;/h2&gt;

&lt;p&gt;By the end of this tutorial, we will have a modal component that can be used anywhere in a React Application. We can create a modal just by passing the children to the component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/p/sandbox/adoring-dew-86v8kl"&gt;CodeSandbox Implementation&lt;/a&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%2F4qmfzdkk6zkxw58tbzq6.gif" 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%2F4qmfzdkk6zkxw58tbzq6.gif" alt="Image description" width="766" height="630"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;They’re cool for rendering UI outside of your current components. We are making a Modal component that shows a message to the user . It should always show up centered in the window. We put the portal higher up in the DOM outside of all out layouts, and render the modal component into it. Then when we want to use it, we just render a  passing the message UI as a prop.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating the Modal Component
&lt;/h2&gt;

&lt;p&gt;In &lt;code&gt;index.html&lt;/code&gt; create a div with id &lt;code&gt;modal&lt;/code&gt;, besides the root div. This is where we will render the modal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Modal Styling
&lt;/h3&gt;

&lt;p&gt;For basic styling, we'll define some CSS rules. The #modal div will serve as the container for our modal content.&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%2F327z1wex9ttj267eig9i.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%2F327z1wex9ttj267eig9i.png" alt="Image description" width="422" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Create Modal.tsx file for the component. Write the boilerplate and props for the 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%2Fpom1gp6o1cytwov1947m.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%2Fpom1gp6o1cytwov1947m.png" alt="Image description" width="590" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We render the children in #modal div using &lt;code&gt;document.getElementById("modal")&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;createPortal&lt;/code&gt; function we can pass the children and the Ref as arguments. So any children will get rendered inside the Modal Div using Ref. Thus we have a reusable component that can take in React Chiukldren and render as our modal.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;return &amp;lt;&amp;gt;{createPortal(children, elRef?.current)}&amp;lt;/&amp;gt;;&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%2Fpkn540zmrepb8py7gmwi.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%2Fpkn540zmrepb8py7gmwi.png" alt="Image description" width="589" height="612"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To close the modal, we listen to all clicks and toggle the state if it's outside the child.&lt;br&gt;
To avoid memory leaks, we have to return a cleanup function and remove our event listener for closing the modal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage of Modal Component
&lt;/h2&gt;

&lt;p&gt;You can import the modal component and create a Boolean State to open and close the Modal. Pass the children, state and closeModal function to the component. The component will mount the children inside the #modal id div.&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%2F24pm5ddllkhkuk22mu9c.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%2F24pm5ddllkhkuk22mu9c.png" alt="Image description" width="800" height="483"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can find the complete TypeScript + React implementation in this &lt;a href="https://codesandbox.io/p/sandbox/adoring-dew-86v8kl"&gt;CodeSandbox&lt;/a&gt; .&lt;/p&gt;

&lt;p&gt;Lakshya Dhariwal&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>tutorial</category>
      <category>react</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
