<?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 Gautamkiran on Medium]]></title>
        <description><![CDATA[Stories by Gautamkiran on Medium]]></description>
        <link>https://medium.com/@gautamkiran123?source=rss-66183f4bd181------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hZL8TGQ8eBF5NCAdy1Jk9g.jpeg</url>
            <title>Stories by Gautamkiran on Medium</title>
            <link>https://medium.com/@gautamkiran123?source=rss-66183f4bd181------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 02:43:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gautamkiran123/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[hydration in React]]></title>
            <link>https://medium.com/@gautamkiran123/hydration-in-react-8e8dff384f93?source=rss-66183f4bd181------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e8dff384f93</guid>
            <category><![CDATA[react-dom]]></category>
            <category><![CDATA[react-js-development]]></category>
            <category><![CDATA[hydration]]></category>
            <category><![CDATA[react-hydration]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Gautamkiran]]></dc:creator>
            <pubDate>Thu, 04 May 2023 07:12:24 GMT</pubDate>
            <atom:updated>2023-08-30T07:47:40.353Z</atom:updated>
            <content:encoded><![CDATA[<p><em>diving deep into the concept of hydration</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c71iP35bcPNPkzume8HzHQ.jpeg" /></figure><p>Before we know about hydration there are certain basic concepts that you need to know about.</p><h4>Client-side rendering (CSR)</h4><p>Client-side rendering means rendering all pages directly in the browser using JavaScript. When all the data fetching is done by the browser instead of a server then it is pretty obvious that the page loading will be slow leading to a bad user experience.</p><p>ReactJS is the perfect example for client-side rendering library.</p><h4>Server-side Rendering (SSR)</h4><p>Server-side rendering uses servers’ request-response process to render the pages using JavaScript. Data fetching is done by the servers and hence it has good user experience. But… why ?</p><p><em>The reason is that in SSR the server first loads the HTML content for the user to atleast have some content on the web page while the JavaScript is being loaded in the background. That is not the case with CSR. CSR always loads a blank page and the markup only gets displayed after the JavaScript has completely loaded. Keep in mind that this is only for the first render i.e. the first time React tree gets rendered from the DOM.</em></p><p>After the DOM has been rendered once it is easier with CSR to keep up with the DOM changes as it much faster and efficient to not make server requests for every DOM change. This difference may not be visible in small applications but the larger the applications get with more number of state changes, the more better it is to use CSR after the complete React tree has been rendered.</p><h3>What is hydration ?</h3><p>Hydration refers to the attaching of React to HTML which was already rendered in a server environment. So we have already rendered our React application on server-side and are hydrating the JavaScript bundle on client-side for smoother user experience and SEO purposes.</p><h4>Difference between rendering and hydration</h4><pre>// index.js<br>import React from &quot;react&quot;;<br>import ReactDOM from &quot;react-dom&quot;;<br>import App from &quot;./App&quot;;<br><br>ReactDOM.render(&lt;App name=&quot;Gautam&quot; /&gt;, document.getElementById(&#39;root&#39;));</pre><pre>// App.js<br><br>import React from &quot;react&#39;;<br><br>const App = ({props}) =&gt; {<br>  return(<br>     &lt;&gt;<br>        &lt;h1&gt; Hello, {props.name}&lt;/h1&gt;<br>     &lt;&gt;<br>)}<br>export {App};<br></pre><p>This above code block gives the result as “Hello, Gautam” as the output on localhost.</p><pre>import React from &quot;react&quot;;<br>import ReactDOM from &quot;react-dom&quot;;<br>import App from &quot;./App&quot;;<br><br>ReactDOM.hydrate(&lt;App name=&quot;Gautam&quot; /&gt;, document.getElementById(&#39;root&#39;));</pre><p>This will throw an warning saying Expected server HTML to contain a matching &lt;h1&gt; in &lt;div&gt; .</p><p>Anyways, this hydrate method was replaced by hydrateRoot() API in the recent React 18 updates. So lets have a look at it whilst we are here.</p><h3>hydrateRoot() API</h3><h4>Syntax</h4><pre>const root = hydrateRoot(domNode, reactNode, ?options)</pre><p>The hydrateRoot() API takes three parameters — namely <em>domNode, reactNode </em>and<em> ?options.</em></p><ul><li><em>domNode</em> — a DOM element that was rendered as the root element in the tree.</li><li><em>reactNode — </em>the React node used to render the existing HTML. This ususally consists of JSX file like App.js etc.</li><li><em>options — </em>it is an object with options for the React root. We will not look into it that much for now.</li></ul><h4>Things to remember</h4><ul><li>hydrateRoot() will compare the rendered content and the server-rendered content and will warn you in case of any mismatches during hydration.</li><li>If the HTML content is not rendered beforehand you can’t use hydrateRoot() .In this case , createRoot() has to be used.</li><li>It is more likely to have only one hydrateRoot() call in your entire React app.</li></ul><h4>Returns</h4><p>createRoot() returns an object with two methods — render and unmount .</p><p>Now let’s have a look at these methods.</p><h4>root.render(reactNode)</h4><p>It is called to update a React component inside the hydrated React DOM tree.</p><pre>root.render(&lt;App/&gt;);<br>// here React will update &lt;App/&gt; component in the hydrated tree</pre><p>It has only one parameter i.e. the React node that we want to be hydrated into the React tree.</p><p>root.render() returns undefined.</p><blockquote>If root.render() is called before the hydration has completed then React will clear the entire server-side rendered HTML and switch the entire root to client-side rendering.</blockquote><h4>root.unmount()</h4><p>It is used to destroy the entire React tree. This method doesn’t have any parameters.</p><pre>root.unmount();</pre><p>An app completely built with React will not have any calls of unmount()</p><p>root.unmount() is mostly useful when the root’s DOM node or any of its ancestors gets removed, we can stop removing everything inside that is related to it by calling the unmount method. In simple words, we are telling React to stop with removing the React nodes after we have removed what was necessary , by calling unmount() method.</p><p>Normally, calling unmount() will unmount all the node in root and remove React from the root DOM node, including any event handlers and state managers.</p><p>Once root.unmount() is called, you cannot call root.render() again as it will throw an error saying Cannot update an unmounted root .</p><h3>Usage</h3><h4>Hydrating server-rendered HTML</h4><pre>import {hydrateRoot} from &quot;react-dom/client&quot;;<br><br>hydrateRoot(document.getElementById(&#39;root&#39;),&lt;App/&gt;);</pre><p>The above code will hydrate the JavaScript logic on the React node with id=&quot;root&quot; with &lt;App/&gt;. React will attach the logic of the &lt;App/&gt; component using client-side rendering to the already server-side rendered HTML .</p><pre>//App.js<br>import React from &quot;react&quot;;<br>import { Counter } from &quot;./Counter.js&quot;;<br>export default function App() {<br>  return(<br>    &lt;&gt;<br>      &lt;h1&gt; Hi there&lt;/h1&gt;<br>      &lt;Counter/&gt;<br>    &lt;/&gt;<br>)}</pre><pre>// index.js<br>import React from &quot;react&quot;;<br>import { hydrateRoot } from &quot;react-dom/client&quot;;<br>import App from &quot;.App.js&quot;;<br><br>hydrateRoot(<br>  document.getElementById(&#39;root&#39;),<br>  &lt;App/&gt;<br>);</pre><pre>// Counter.js<br>import React from &quot;react&quot;;<br>import {useState} from &quot;react&quot;;<br><br>export const Counter = () =&gt; {<br>const [ count, setCount ] = useState(0);<br>return(<br>  &lt;button onClick={()=&gt; setCount(count+1)}&gt; You clicked the counter {count} times &lt;/button&gt;<br>)}</pre><p>This code will make a counter in which the HTML will be loaded on the server-side while the React logic will load in the client-side.</p><blockquote>We are keeping the user in an illusion for some time while we load the JS logic while using hydration to improve user experience. Hence it is important that the React tree rendered on the server-side and the client-side is the same. Otherwise the developer will run into hydration errors.</blockquote><blockquote>The most common causes leading to hydration errors include:</blockquote><blockquote>- Extra whitespace (like newlines) around the React-generated HTML inside the root node.</blockquote><blockquote>- Using checks like typeof window !== &#39;undefined&#39; in your rendering logic.</blockquote><blockquote>- Using browser-only APIs like <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia">window.matchMedia</a> in your rendering logic.</blockquote><blockquote>- Rendering different data on the server and the client.</blockquote><p>Read more about hydrations errors here:</p><p><a href="https://nextjs.org/docs/messages/react-hydration-error">react-hydration-error | Next.js</a></p><h4>Hydrating an entire document</h4><p>Apps made completely from React are capable of rendering the entire document as JSX, and hence the entire document can be hydrated at once.</p><p>To hydrate the entire document , pass it as a React node.</p><pre>import { hydrateRoot} from &quot;react-dom/client&quot;;<br>import { App } from<br><br>hydrateRoot(document, &lt;App/&gt;);</pre><h4>Suppressing unavoidable hydration mismatch errors</h4><p>If a single element’s attribute or text content is unavoidably different between the server and the client (for example, a timestamp), you may silence the hydration mismatch warning.</p><p>To silence hydration warnings on an element, add suppressHydrationWarning={true}.</p><blockquote>This method is considered as an escape hatch and only works a level deep. Hence it is recommended not to use it too much.</blockquote><h4>Handling different client and server content</h4><p>If you intentionally need to render something different on the server and the client, you can do a two-pass rendering. Components that render something different on the client can read a <a href="https://react.dev/reference/react/useState">state variable</a> like isClient, which you can set to true in an <a href="https://react.dev/reference/react/useEffect">Effect</a>.</p><p>That’s all about hydration for now, if you like my content make sure to like this blog, comment your feedbacks and follow me on Medium. It keeps me motivated to write more consistently while improving from the feedbacks given.</p><p>Till then, see ya …</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e8dff384f93" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Portals in React]]></title>
            <link>https://medium.com/@gautamkiran123/react-dom-18-2-0-createportal-api-7acbce0d502?source=rss-66183f4bd181------2</link>
            <guid isPermaLink="false">https://medium.com/p/7acbce0d502</guid>
            <category><![CDATA[createportal]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[portal]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[portals-in-react]]></category>
            <dc:creator><![CDATA[Gautamkiran]]></dc:creator>
            <pubDate>Tue, 25 Apr 2023 18:21:31 GMT</pubDate>
            <atom:updated>2023-05-04T07:09:23.572Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Let’s look inside the world of portals</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c71iP35bcPNPkzume8HzHQ.jpeg" /></figure><p>Portals were introduced in React in order to render the children into a DOM node that is completely unrelated to the original DOM node of the children.</p><p>Though it may sound confusing a bit at first as to why the children have to be ported from one node to another when we can just import the components anywhere we want, this isn’t always the case in your applications. There are certain use cases that createPortal fits perfectly into.</p><p>In this blog we will look into the nuances of portals and the createPortal API. These concepts are a bit tough to understand but I will try my best to simplify them and make them easily understandable for you.</p><h3>How to use createPortal reference ?</h3><p>Import createPortal in your file from “react-dom”.</p><pre>import { createPortal } from &quot;react-dom&quot;;</pre><p>createPortal takes two parameters always</p><ul><li><em>children — anything that can be rendered in React ranging from a piece of JSX code to a React component or arrays, strings etc.</em></li><li><em>domNode — this should contain the DOM node where we want to render the children specified in the other parameter. This node should already exist in the DOM.</em></li></ul><blockquote>Passing a different DOM node while updating causes the portal to be recreated.</blockquote><blockquote>Events from portals propagate through React tree rather than a DOM tree. Hence it may be problematic in the case of <strong>Event Bubbling.</strong></blockquote><h3>Event Bubbling through Portals</h3><p>Event though portals can be created anywhere they still behave as a React child and features like context work exactly the same way as they are supposed to work for normal children as the children are still a part of React Tree rather than the DOM tree.</p><p>Event bubbling refers to the property of the DOM where an event in the children are passed on or propagated to the parents until it gets to the root element.</p><pre>&lt;html&gt;<br>  &lt;body&gt;<br>    &lt;div&gt;<br>      &lt;button onClick={clickHandler}&gt;<br>         Click me!<br>      &lt;/button&gt;<br>    &lt;/div&gt;<br>  &lt;/body&gt;<br>&lt;/html&gt;</pre><p>In the above example, when the &lt;button&gt; element is clicked, a clickHandler event is called. Rightly so, in DOM this event will get propagated to the &lt;div&gt;,&lt;body&gt; and &lt;html&gt; elements. This is the simplest representation of Event Bubbling.</p><p>Portals support this event bubbling too as any node accessed through portals will read the events even though the component is in a completely unrelated node in the DOM.</p><h3>Usage</h3><h4>Rendering to different parts of DOM</h4><p>The basic use of portals is to render an element or component (children) in a different part of DOM. For example, a modal or tooltip which appears outside and above the rest of the page.</p><pre>import { createPortal } from &quot;react-dom&quot;;<br><br>export const Card = () =&gt; {<br>  return(<br>    &lt;div&gt;<br>      &lt;div&gt;This text is rendered inside the Card component&lt;/div&gt;<br>      {createPortal( <br>          &lt;div&gt;This is rendered as a portal&lt;/div&gt;, <br>           document.body )<br>      }<br>    &lt;/div&gt;<br>  )<br>}<br>/* here the text inside createPortal is rendered in the body whereas the <br>above div will get rendered as a part of Card component */ </pre><blockquote>Portals only change the physical placement of the DOM node so the JSX rendered into the portal acts as the child of the React component that renders it.</blockquote><h4>Rendering a Modal dialog with portal</h4><p>There are times when the parent component has CSS properties like overflow: hidden which make having a Modal dialog to appear pretty messy.</p><p>Portals provide great assistance in this, as by using portals for the Modal it doesn’t get rendered as the child of the parent node with overflow: hidden property.</p><p>It is important to take care of accessibility when using portals for instance managing the <strong>keyboard focus</strong> while jumping to portal components and returning back to the original component</p><blockquote><strong>Keyboard focus </strong>: It is nothing but the current element in the DOM that was selected to accept input from the keyboard. For example, if a Modal dialog is opened with a button then after closing the Modal it is advisable to have the keyboard focus back on the button only. This can get tricky as React always keeps rendering on changes thus changing the nodes too, but this can be taken care of easily. We can talk about this in another blog sometime later.</blockquote><h4>Rendering React components into non-React server markup</h4><p>Portals can be pretty useful when it comes to adding interactivity to apps which are not made of React. Portals treat the whole application as a single React tree which makes adding React to static or server-side rendered applications which are not made from React.</p><p>Interactivity can also be achieved using createRoot API in React but Portals do the job better. Why ? Let’s look into that matter then.</p><p>So basically, you can call createRoot multiple times to create root for each top level UI in React which can be rendered using root.render() . But this creates a different React tree and is not efficient while rendering. Using hydrateRoot can be another alternative but portals are still better then them as hydrateRoot still hydrates (reuse) the DOM instead of destroying and re-creating it while portals simply consider the whole application as a single React application.</p><h4>Rendering React Components into non-React DOM nodes</h4><p>Portals are so flexible in usage that you can use them to render React components even in DOM nodes that are not in React. The only thing to be take care of is storing the DOM node of the third-party widgets being used in which React components are added.</p><p>For example, you can have popups in a map which is built by a third party without any react.</p><p>That’s all about portals for now, if you like my content make sure to like this blog, comment your feedbacks and follow me on Medium. It keeps me motivated to write more consistently while improving from the feedbacks given.</p><p>Till then, see ya …</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7acbce0d502" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Import/Export in React]]></title>
            <link>https://medium.com/@gautamkiran123/import-export-in-react-23c09940776?source=rss-66183f4bd181------2</link>
            <guid isPermaLink="false">https://medium.com/p/23c09940776</guid>
            <category><![CDATA[react18]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[import-export]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[js]]></category>
            <dc:creator><![CDATA[Gautamkiran]]></dc:creator>
            <pubDate>Mon, 24 Apr 2023 12:22:58 GMT</pubDate>
            <atom:updated>2023-04-24T12:22:58.299Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Diving deep into the concept of importing and exporting in React</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kN4IIIcp9hhwN-5xlV1K4A.jpeg" /></figure><p>Components are the soul of React. Their sole purpose was to build user interfaces in order to achieve code reusability and interactivity, all while writing clean code.</p><p>Components are nothing but just functions returning markup. Earlier when interactivity was a “good-to-have” feature, developers used HTML, CSS and JavaScript in order to achieve it but now that it is a necessity in most of the apps and websites, JSX format is used which contains defined functions that return markup along. JSX is stricter, faster and better than using vanilla JavaScript to achieve interactivity.</p><p>But now the question arises — where does import and export play a role in these components. In React, components are re-used a lot of times and it is one of the core advantages of using this library. To achieve this reusability we use the import and export features in React.</p><h3>How to use them?</h3><p><strong>Step 1 :</strong> <strong>Define your function in a file and export it using default or named exports.</strong></p><pre>// using default export on a Card component<br><br>export default const Card = () =&gt; {<br>  return(<br>    &lt;div&gt;<br>      &lt;h1&gt;This is the card component&lt;/h1&gt;<br>    &lt;/div&gt;<br>)<br><br>// using named export on a Card component<br>export const Card = () =&gt; {<br>  return(<br>    &lt;div&gt;<br>      &lt;h1&gt;This is the card component&lt;/h1&gt;<br>    &lt;/div&gt;<br>)</pre><ul><li>We will look into the differences of named and default exports later in this blog, so be calm if you don’t understand them now.</li></ul><p><strong>Step 2 : Import the component in the file that you want to use the Card component</strong></p><pre>// while using default exports <br>import Card from &quot;./components/Card.jsx&quot;; <br><br>// notice that the path name can also be &quot;./components/Card&quot; as JSX reads it<br>// as a JSX file if the file format isn&#39;t mentioned<br><br>// while using named exports <br>import { Card } from &quot;./components/Card.jsx&quot;;<br></pre><p>See that was pretty simple, wasn’t it?</p><p>But what is the difference between default and named exports? And why do we need both? Let’s learn about them now.</p><h3><strong>Default v/s Named exports</strong></h3><p>Both the types of exports can be used for exporting components, files, images, etc. But the import method depends on the type of export method used i.e. the wrapping of component in { } while importing using named exports and not wrapping the component while using default exports.</p><p>But the main difference between them is that default exports can only be used once in a file whereas named exports can be used as many times as one wishes in the file.</p><p>Adding to the above difference, default exports can be imported using any name since we already know that there is only one default export in the particular file whereas you need to have the same name as the component while importing named exports. P.S. There is a reason they are termed as “named” exports.</p><h3>What if I want to do multiple exports/imports ?</h3><p>Well, it is common sense to use named exports for functions to use more than one import in the files. There wouldn’t be a problem if you use both default and named exports in the same file</p><pre>// ./components/Card.jsx<br>export const Card = () =&gt; {}<br><br>// ./components/Navbar.jsx<br>export default Navbar = () =&gt; {}</pre><pre>// ./App.js<br>import Nav from &quot;./components/Navbar.jsx&quot;;<br>// notice how Navbar is imported as Nav using default import<br>import { Card } from &quot;./components/Card.jsx&quot;;<br><br>export default App(){<br>  return (<br>    &lt;&gt;<br>      &lt;Nav/&gt; <br>      &lt;Card/&gt;<br>    &lt;/&gt;<br>)<br>  </pre><blockquote><strong>Note:</strong> It is recommended to use named exports wherever possible as it is easier to debug. Also it is better if you name your components wisely and export/import them using the same name. Named imports can also be used multiple times in a file which makes them pretty handy.</blockquote><h3>How to import CSS ?</h3><p>Well, React also takes care of the way CSS files can be imported. While importing CSS files , images etc. it directly imports from the file without having any export defined in the root file simply because these files can’t have any export because import/export are part of the JavaScript model and not CSS or image file formats.</p><pre>import styles from &quot;./Card.css&quot;;<br><br>/* here the entire &#39;Card.css&#39; file will be mapped to the `styles` keyword, <br>so the CSS selectors used will be considered inside the `styles` component<br>and can be accessed while styling using `{styles.&lt;className&gt;}`*/ <br><br>import img1 from &quot;./assets/img.jpeg&quot;;<br><br>/* here the image will be mapped to the `img1` keyword and React will read <br>it as img1 is nothing but the image in the source of the file mentioned while<br>importing */</pre><h4>In my opinion, always make named exports/imports for components that you as a developer are creating but keep the default file exports that are present beforehand when you create a React App as they are i.e. as default exports .</h4><p>That’s it for this time in this blog, if you like my content make sure to like this blog, comment your feedbacks and follow me on Medium. It keeps me motivated to write more consistently while improving from the feedbacks given.</p><p>Till then, see ya …</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=23c09940776" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>