<?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: Das Sameer</title>
    <description>The latest articles on DEV Community by Das Sameer (@dassameer07).</description>
    <link>https://dev.to/dassameer07</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%2F3160294%2F13d7b103-54f9-4e9f-9438-bce776279fcc.png</url>
      <title>DEV Community: Das Sameer</title>
      <link>https://dev.to/dassameer07</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dassameer07"/>
    <language>en</language>
    <item>
      <title>From Root to Anywhere: How React Portals Work</title>
      <dc:creator>Das Sameer</dc:creator>
      <pubDate>Thu, 15 May 2025 09:48:27 +0000</pubDate>
      <link>https://dev.to/dassameer07/from-root-to-anywhere-how-react-portals-work-563g</link>
      <guid>https://dev.to/dassameer07/from-root-to-anywhere-how-react-portals-work-563g</guid>
      <description>&lt;p&gt;&lt;a href="https://dev.tourl"&gt;&lt;/a&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br&gt;
React normally renders components into a specific DOM hierarchy. But what if you need to render something outside of that? That’s where React Portals come in.&lt;br&gt;
In this post, we’ll look at what a Portal is when to use it, and how to implement one.&lt;br&gt;
&lt;strong&gt;What Is a React Portal?&lt;/strong&gt;&lt;br&gt;
i.React Portals provide a way to render child components outside of their parent's DOM hierarchy. This functionality is useful in scenarios where a component needs to visually "break out" of its container, such as with modals, tooltips, or pop-up menus. &lt;/p&gt;

&lt;p&gt;ii.The ReactDOM.createPortal method is used to achieve this, taking two arguments: the child element to be rendered and the DOM node where it should be rendered.&lt;/p&gt;

&lt;p&gt;syntax-ReactDOM.createProtal(children,container)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For Example:&lt;/strong&gt;&lt;br&gt;
Let’s say we want a modal to appear above all content, even if it's deeply nested in the React tree.&lt;br&gt;
&lt;strong&gt;HTML Structure&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
&amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div id="modal"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;React Component&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ReactDOM from "react-dom"
import { X } from 'lucide-react';  // this is for close icon ,it is not 
                                       belongs to react portals concept

const Modal=({children})=&amp;gt;{    

    return ReactDOM.createPortal(
        &amp;lt;div className=" fixed bottom-0 top-0 overflow-hidden left-0 right-0 bg-[#00000086] flex justify-center items-center  z-50 "&amp;gt;
           &amp;lt;div className="bg-white relative   rounded-[10px] px-3 max-lg:absolute max-lg:bottom-0  min-w-[300px] lg:max-w-[600px] overflow-auto max-lg:w-full  pt-4 min-h-[300px] max-lg:h-[90vh] lg:max-h-[400px]"&amp;gt;
                &amp;lt;button  className="absolute right-2 top-2 max-lg:hidden inline"&amp;gt;&amp;lt;X/&amp;gt;&amp;lt;/button&amp;gt;
                {children}

           &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;,
        document.getElementById("modal")

    )
}

export default Modal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;How to use It&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Modal&amp;gt; 
    &amp;lt;h3&amp;gt;This is modal Content&amp;lt;/h3&amp;gt;
&amp;lt;/Modal&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;When to use It?&lt;/strong&gt;&lt;br&gt;
 React Portals used in below scenario&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Modals and Dialogs&lt;/li&gt;
&lt;li&gt;Notification Bar&lt;/li&gt;
&lt;li&gt;Tooltips and dropdowns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
This is particularly valuable for creating elements like modals, tooltips, notifications, or dropdowns that need to visually break out of their container's boundaries to avoid styling conflicts, layout issues  overflow-hidden or z-index problems.&lt;/p&gt;

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