<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Rahul Kumar Mishra on Medium]]></title>
        <description><![CDATA[Stories by Rahul Kumar Mishra on Medium]]></description>
        <link>https://medium.com/@r4hul?source=rss-1153e195b452------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*K1REzJuGYCs5Lvpkp6Qytw.png</url>
            <title>Stories by Rahul Kumar Mishra on Medium</title>
            <link>https://medium.com/@r4hul?source=rss-1153e195b452------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 01:07:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@r4hul/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to use React Portal]]></title>
            <link>https://medium.com/@r4hul/how-to-use-react-portal-dff5b2582efe?source=rss-1153e195b452------2</link>
            <guid isPermaLink="false">https://medium.com/p/dff5b2582efe</guid>
            <category><![CDATA[javascript-tips]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript-development]]></category>
            <dc:creator><![CDATA[Rahul Kumar Mishra]]></dc:creator>
            <pubDate>Mon, 24 Feb 2020 20:06:01 GMT</pubDate>
            <atom:updated>2020-02-24T20:06:01.319Z</atom:updated>
            <content:encoded><![CDATA[<h3>React Portal — In Action</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4LCpqOv21fklQA9Ofpda2w.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@paan_azam13?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Farhan Azam</a> on <a href="https://unsplash.com/s/photos/portal?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In this article, we are going to understand and render react children outside the react app root node using react-portal.</p><p>According to the official <a href="https://reactjs.org/docs/portals.html"><em>React page</em></a><em>.</em></p><blockquote>“Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.”</blockquote><h4>Usage</h4><p>Our main goal is to render the react component as a sibling of the root component. For this, add a new element &lt;div id=”react-portal”&gt;&lt;/div&gt; as a sibling to theroot level element.</p><pre>// public/index.html</pre><pre>&lt;div id=&quot;root&quot;&gt;&lt;/div&gt; // root level element</pre><pre>&lt;div id=&quot;react-portal&quot;&gt;&lt;/div&gt;</pre><p>Now we are going to make the Portal component. It will take two arguments.</p><pre>ReactDOM.createPortal(child, container)</pre><p>In this, the first argument(child) is any renderable react child and the second argument(container) is a DOM element. With this, we are going to create a new file Portal.js.</p><pre>// app/Portal.js</pre><pre>import React from &#39;react&#39;;<br>import ReactDOM from &#39;react-dom&#39;;</pre><pre>function Portal() {<br>  return ReactDOM.createPortal(&lt;div&gt;<br>         Make me a portal component<br>&lt;/div&gt;,document.getElementById(&#39;react-portal&#39;)<br>);<br>}</pre><pre>export default Portal</pre><p>The final step is to render the Portal.js file to the main App.js file.</p><pre>// src/App.js</pre><pre>import React from &#39;react&#39;;<br>import Portal from &#39;./Portal&#39;;</pre><pre>function App() {<br>  return (<br>    &lt;div className=&quot;App&quot;&gt;<br>    &lt;h1&gt;React Portal&lt;/h1&gt;<br>    &lt;Portal /&gt; // Portal component<br>    &lt;/div&gt;<br>  );<br>}</pre><pre>export default App;</pre><p>This is the DOM using react-portal. Here we can see the react-portal is rendered outside the main root component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RRC6QmzYOIBqcX9Pf_ES3Q.png" /><figcaption>react-portal in action</figcaption></figure><h4>When to use?</h4><p>A typical use case is when a parent component has an overflow or z-index properties. For example in modals , tooltips, widgets, sidebar etc. the child components can <strong><em>visually break-out</em></strong> the DOM structure(due to the use of overflow: hidden or z-index properties).</p><h4>Conclusion</h4><ul><li>React-portal only affects the DOM structure, not the React components tree.</li><li>React children rendered through react-portal are still under React’s control.</li><li>Event bubbling will work as normal to there parent react component.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dff5b2582efe" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>