<?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: Hari Kotha</title>
    <description>The latest articles on DEV Community by Hari Kotha (@harireddy7).</description>
    <link>https://dev.to/harireddy7</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%2F407354%2Ff33c417e-bfec-4783-8149-a7ae1fdaba66.jpg</url>
      <title>DEV Community: Hari Kotha</title>
      <link>https://dev.to/harireddy7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/harireddy7"/>
    <language>en</language>
    <item>
      <title>Set environment variables in docker</title>
      <dc:creator>Hari Kotha</dc:creator>
      <pubDate>Mon, 08 May 2023 12:19:27 +0000</pubDate>
      <link>https://dev.to/harireddy7/set-environment-variables-in-docker-274h</link>
      <guid>https://dev.to/harireddy7/set-environment-variables-in-docker-274h</guid>
      <description>&lt;h2&gt;
  
  
  How to use Env variables in Docker &amp;amp; docker-compose
&lt;/h2&gt;




&lt;h2&gt;
  
  
  Dockerfile
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. ENV
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;ENV&lt;/code&gt; command to set env variable in Dockerfile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; username=barryallen&lt;/span&gt;
echo $username // barryallen

&lt;span class="c"&gt;# Build this image and run it as a container&lt;/span&gt;
docker build -t docker-env .

docker run docker-env
&lt;span class="c"&gt;# logs "barryallen"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Environment variables defined with ENV command are persisted in multiple containers running the same image. These can be overridden using run command with -e flag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="c"&gt;# Use -e or --env to override env variables in Dockerfile via build command&lt;/span&gt;
docker run -e/--env "username=clark kent" docker-env

&lt;span class="c"&gt;# Use --env-file to use .env file to override env variables in Dockerfile&lt;/span&gt;
docker run --env-file .env docker-env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this to override a variables in a specific container. Other containers running without -e flag still uses env variable defined in Dockerfile&lt;/p&gt;

&lt;h3&gt;
  
  
  2. ARG
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;ARG&lt;/code&gt; command to use dynamic values that are only necessary while building the image and not needed while running it as container&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;ARG&lt;/span&gt;&lt;span class="s"&gt; location="Gotham City"&lt;/span&gt;

echo $location // Gotham City

&lt;span class="c"&gt;# build the image &amp;amp; run in container&lt;/span&gt;
docker build -t docker-env .

docker run docker-env
&lt;span class="c"&gt;# location will not be available as ENV variable in containers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ARG variable can be dynamically passed in build command as 👇&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;docker build -t app-latest --build-arg "location=Watch Tower"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Use ARG as ENV
&lt;/h3&gt;

&lt;p&gt;We can persist &lt;code&gt;ARG&lt;/code&gt; variables by mapping them to &lt;code&gt;ENV&lt;/code&gt; variables in Dockerfile.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;ARG&lt;/span&gt;&lt;span class="s"&gt; version&lt;/span&gt;
&lt;span class="k"&gt;ENV&lt;/span&gt;&lt;span class="s"&gt; VERSION=${version}&lt;/span&gt;
echo $VERSION

docker build -t docker-env . --build-arg "version=1.0.1"

docker run docker-env
&lt;span class="c"&gt;# 1.0.1&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  docker-compose
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. environment
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;docker-env:
    build:
        context: ./
    environment:
        - VERSION=1.0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can set environment variables in a service’s containers with the &lt;code&gt;environment&lt;/code&gt; attribute in your Compose file.&lt;/p&gt;

&lt;p&gt;This works the same way as &lt;code&gt;docker run -e VARIABLE=VALUE docker-image&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2. env_file
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;docker-env:
    build:
        context: ./
    env_file: .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead of hardcoding env variables in your compose file, you can add those keys in a .env file and provide it to &lt;code&gt;env_file&lt;/code&gt; attribute.&lt;/p&gt;

&lt;p&gt;This works the same way as &lt;code&gt;docker run --env-file=FILE docker-image&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When both &lt;code&gt;env_file&lt;/code&gt; and &lt;code&gt;environment&lt;/code&gt; are set for a service, values set by &lt;code&gt;environment&lt;/code&gt; have precedence.&lt;/p&gt;




&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;h2&gt;
  
  
  cheers :) 
&lt;/h2&gt;

&lt;p&gt;Checkout my other blogs&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/harireddy7/react-portals-intro-71e"&gt;React Portals&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/harireddy7/yarn-essentials-1307"&gt;Yarn essentials&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/harireddy7/simple-intro-to-ssh-2mo5"&gt;Simple intro to SSH&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>docker</category>
      <category>dockercompose</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Simple intro to SSH</title>
      <dc:creator>Hari Kotha</dc:creator>
      <pubDate>Wed, 19 Apr 2023 08:17:15 +0000</pubDate>
      <link>https://dev.to/harireddy7/simple-intro-to-ssh-2mo5</link>
      <guid>https://dev.to/harireddy7/simple-intro-to-ssh-2mo5</guid>
      <description>&lt;p&gt;&lt;strong&gt;SSH authentication is based on private and public keys pair.&lt;/strong&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%2Fy6fmz8iflx9gwmd7ztk6.jpg" 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%2Fy6fmz8iflx9gwmd7ztk6.jpg" alt="intro-to-ssh-cover" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;User generates a key pair consisting of a &lt;em&gt;private key&lt;/em&gt; and a &lt;em&gt;public key&lt;/em&gt;. The private key is kept securely on the user's computer, while the public key is uploaded to the server.&lt;/p&gt;

&lt;p&gt;When the user tries to connect to the server, the server sends a challenge to the client. The challenge is a &lt;em&gt;random string&lt;/em&gt; that is encrypted with the server's public key.&lt;/p&gt;

&lt;p&gt;The client receives the challenge and decrypts it using its private key. The client sends the decrypted challenge back to the server.&lt;/p&gt;

&lt;p&gt;The server verifies that the decrypted challenge matches the original challenge. If it does, the server grants access to the client. else server rejects the connection.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It's the user that generates their own key pair not the server. The server doesn't send the private key to the user, as that would be a security risk. The private key should always remain on the user's computer and never be shared with anyone else.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>ssh</category>
      <category>webdev</category>
      <category>programming</category>
      <category>security</category>
    </item>
    <item>
      <title>Yarn essentials</title>
      <dc:creator>Hari Kotha</dc:creator>
      <pubDate>Mon, 10 Apr 2023 18:15:27 +0000</pubDate>
      <link>https://dev.to/harireddy7/yarn-essentials-1307</link>
      <guid>https://dev.to/harireddy7/yarn-essentials-1307</guid>
      <description>&lt;h2&gt;
  
  
  Yarn essentials
&lt;/h2&gt;

&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;Yarn is a JavaScript package manager used to install/download libraries from npm repository&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%2Ftevtuhstdeu666ddqvc7.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%2Ftevtuhstdeu666ddqvc7.png" alt="yarn essentials cover image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  yarn install
&lt;/h2&gt;

&lt;p&gt;yarn install is used to download libraries those are listed under dependencies object in your &lt;code&gt;package.json&lt;/code&gt; file.&lt;/p&gt;

&lt;h2&gt;
  
  
  How yarn installs the packages
&lt;/h2&gt;

&lt;p&gt;When yarn install is executed, Yarn will fetch the package from the registry which is an npm public repository which contain all the JavaScript libraries contributed by various developers all over the world.&lt;/p&gt;

&lt;p&gt;Yarn will download all the files related to that package and install it in your project's &lt;strong&gt;&lt;code&gt;node_modules&lt;/code&gt;&lt;/strong&gt; folder. It will also update your &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt; file to include the new package as a dependency. yarn.lock file will also get updated with latest dependency tree. Yarn will make sure to install the package's dependencies if they are not already installed in node_modules.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="nx"&gt;yarn&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="nx"&gt;registry&lt;/span&gt;
&lt;span class="c1"&gt;// https://registry.yarnpkg.com&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Any package installation will direct this 👆 registry url. This will again gets directed to npm’s registry &lt;code&gt;registry.npmjs.org&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installing scoped private packages
&lt;/h2&gt;

&lt;p&gt;So, Installing packages that are already present in the yarn/npm registry is straight forward. But many a times, while working on a big enterprise applications, we tend to have few libraries which are private to that organisation. &lt;/p&gt;

&lt;p&gt;Such libraries are hosted and managed using GitHub package registry - &lt;code&gt;https://npm.pkg.github.com&lt;/code&gt;. So to install any private or scoped(@org-name) packages, we will have to point our yarn to this registry.&lt;/p&gt;

&lt;p&gt;Also for security reasons, we will have to authenticate with GitHub to verify that we have read/install permissions. We will have to provide auth token (access token) with read/install packages scopes/permissions while installing these packages.&lt;/p&gt;

&lt;p&gt;This can be done in 2 ways&lt;/p&gt;

&lt;p&gt;1️⃣ Using —registry flag while installing a private library&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// update github personal access token&lt;/span&gt;
&lt;span class="nx"&gt;yarn&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nx"&gt;npmAuthToken&lt;/span&gt; &lt;span class="nx"&gt;YOUR_GITHUB_PAT&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;scope&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;org&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;

&lt;span class="c1"&gt;// provide github registry&lt;/span&gt;
&lt;span class="nx"&gt;yarn&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;org&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="kr"&gt;private&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="kr"&gt;package&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="nx"&gt;registry&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//npm.pkg.github.com&lt;/span&gt;
&lt;span class="c1"&gt;// eg: yarn add @twitter/twitter-ui -- registry=https://npm.pkg.github.com&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;As per example above, this will inform yarn to search and install the &lt;code&gt;twitter-ui&lt;/code&gt; package from twitter’s github registry.&lt;/p&gt;

&lt;p&gt;2️⃣ Use &lt;code&gt;.npmrc&lt;/code&gt; file to add the additional configurations&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;npmrc&lt;/span&gt;

&lt;span class="c1"&gt;//npm.pkg.github.com/:_authToken=YOUR_GITHUB_TOKEN_1&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;org1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;registry&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//npm.pkg.github.com&lt;/span&gt;

&lt;span class="c1"&gt;//npm.pkg.github.com/:_authToken=YOUR_GITHUB_TOKEN_2&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;org2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nx"&gt;registry&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;https&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="c1"&gt;//npm.pkg.github.com&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add this at the root of your project where package.json is present. When yarn install/yarn add is executed, yarn will point to those specific repos while installing private/scoped libraries.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="c1"&gt;//npm.pkg.github.com/org1/:_authToken=YOUR_GITHUB_TOKEN_1&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We can also provide authToken specific to scoped repository like this 👆&lt;/p&gt;

&lt;h2&gt;
  
  
  Other Commands
&lt;/h2&gt;

&lt;h2&gt;
  
  
  yarn add
&lt;/h2&gt;

&lt;p&gt;yarn add is used to install a specific library to your project&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="nx"&gt;yarn&lt;/span&gt; &lt;span class="nx"&gt;add&lt;/span&gt; &lt;span class="nx"&gt;lodash&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will install latest version of lodash and add the files to your node_modules and create an entry under dependencies in package.json and updates yarn.lock file&lt;/p&gt;

&lt;h2&gt;
  
  
  yarn remove
&lt;/h2&gt;

&lt;p&gt;yarn remove will deletes the files of a specific library from the node_modules folder and removes the entry in package.json and updates yarn.lock as well&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="nx"&gt;yarn&lt;/span&gt; &lt;span class="nx"&gt;remove&lt;/span&gt; &lt;span class="nx"&gt;lodash&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  yarn cache clean
&lt;/h2&gt;

&lt;p&gt;yarn caches the packages that we install into a .cache directory. we can find it in linux at &lt;code&gt;.cache/yarn/v6&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To fetch the latest files of the version that is being cached, we will have to clean the cache first.&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;ls | grep package-name&lt;/code&gt; command to check for the cached version of any package or use &lt;code&gt;yarn cache list --pattern "gulp|grunt"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Use &lt;strong&gt;yarn cache clean package-name&lt;/strong&gt; to delete the cached version of any package or use &lt;strong&gt;yarn cache clean&lt;/strong&gt; to delete entire global cache.&lt;/p&gt;
&lt;h2&gt;
  
  
  yarn config list
&lt;/h2&gt;

&lt;p&gt;yarn config list displays the current configuration of yarn which contain details about registry, any auth tokens for scoped GitHub repos&lt;/p&gt;
&lt;h2&gt;
  
  
  yarn config set
&lt;/h2&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;

&lt;span class="nx"&gt;yarn&lt;/span&gt; &lt;span class="nx"&gt;config&lt;/span&gt; &lt;span class="kd"&gt;set&lt;/span&gt; &lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;license&lt;/span&gt; &lt;span class="nx"&gt;MIT&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This command sets the init-license key to ‘MIT’&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>yarn</category>
      <category>npm</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>React Portals: Intro</title>
      <dc:creator>Hari Kotha</dc:creator>
      <pubDate>Sun, 22 Aug 2021 15:52:38 +0000</pubDate>
      <link>https://dev.to/harireddy7/react-portals-intro-71e</link>
      <guid>https://dev.to/harireddy7/react-portals-intro-71e</guid>
      <description>&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%2Foc7yi6avk7bqavn7ma1j.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%2Foc7yi6avk7bqavn7ma1j.png" alt="Image description" width="800" height="240"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article will cover -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic intro to Portals in React.js&lt;/li&gt;
&lt;li&gt;Why &amp;amp; How to use react portals&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  React Portals
&lt;/h2&gt;

&lt;p&gt;Renders react components outside of root DOM element&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Portal
&lt;/h2&gt;

&lt;p&gt;React renders all its components inside a single root DOM element (ideally of id = "root").&lt;/p&gt;

&lt;p&gt;If instead, we have a use-case where we want to render a component outside of this root element, we use a concept called &lt;strong&gt;React Portals.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create Portals
&lt;/h2&gt;

&lt;p&gt;Ideally, to bootstrap a react app, we use ReactDOM.render method to render our react app to root element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;SomeReactComponent&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, To create a portal we use ReactDOM.createPortal method which takes two parameters&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React Component or JSX&lt;/li&gt;
&lt;li&gt;HTML DOM element
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"portal"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createPortal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Portal goes here&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;,&lt;/span&gt;
    &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;portal&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why to use React Portals
&lt;/h2&gt;

&lt;p&gt;We can use Portals while creating a Modal or Popup as these occupy space outside of the normal element ordering/stacking. Modals sometimes cover the whole page for example take a look at the below code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Wrapper&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;maxWidth:&lt;/span&gt; &lt;span class="err"&gt;'250&lt;/span&gt;&lt;span class="na"&gt;px&lt;/span&gt;&lt;span class="err"&gt;',&lt;/span&gt; &lt;span class="na"&gt;zIndex:&lt;/span&gt; &lt;span class="err"&gt;1,&lt;/span&gt; &lt;span class="na"&gt;position:&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="na"&gt;relative&lt;/span&gt;&lt;span class="err"&gt;'&lt;/span&gt; &lt;span class="err"&gt;}}&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;Modal&lt;/span&gt; &lt;span class="na"&gt;open&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;zIndex:&lt;/span&gt; &lt;span class="err"&gt;1000,&lt;/span&gt; &lt;span class="na"&gt;postion:&lt;/span&gt; &lt;span class="err"&gt;'&lt;/span&gt;&lt;span class="na"&gt;fixed&lt;/span&gt;&lt;span class="err"&gt;',&lt;/span&gt; &lt;span class="na"&gt;top:&lt;/span&gt; &lt;span class="err"&gt;0,&lt;/span&gt; &lt;span class="na"&gt;left:&lt;/span&gt; &lt;span class="err"&gt;0&lt;/span&gt; &lt;span class="err"&gt;}}&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Some content inside Modal&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/Modal&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Wrapper&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;OtherWrapper&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;zIndex:&lt;/span&gt; &lt;span class="err"&gt;100&lt;/span&gt; &lt;span class="err"&gt;}}&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;some other content&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/OtherWrapper&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As per the example above, &lt;em&gt;Modal&lt;/em&gt; is being rendered inside a &lt;em&gt;Wrapper&lt;/em&gt; that has max-width limitation and a z-index set to 1. That means even though Modal has a z-index of 1000, it cannot be on top of everything because &lt;em&gt;OtherWrapper&lt;/em&gt; has a higher z-index than &lt;em&gt;Wrapper&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Hence, Modal(z-index=1000) will be still under OtherWrapper(zIndex=100) as Modal is inside Wrapper(z-index=1).&lt;/p&gt;

&lt;p&gt;To overcome these kinds of issues, we can implement a Portal to render Modal -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"portal-root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;return ReactDOM.createPortal(
    &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
        Modal content goes here
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;,
    document.getElementById('portal-root')
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will render the Modal component inside &lt;em&gt;portal-root&lt;/em&gt; DOM element outside the root element. However, this is will be kept as usual in React component tree &amp;amp; events are listened &amp;amp; executed as before.&lt;/p&gt;

&lt;p&gt;Thanks for checking!&lt;br&gt;
Stay safe :)&lt;/p&gt;

&lt;p&gt;References:&lt;br&gt;
&lt;a href="https://reactjs.org/docs/portals.html"&gt;https://reactjs.org/docs/portals.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>portals</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
