<?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 Sujit Singh on Medium]]></title>
        <description><![CDATA[Stories by Sujit Singh on Medium]]></description>
        <link>https://medium.com/@sujitsingh_55772?source=rss-1e34b8b45b12------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Pe7xa1uVNhrAoPhG.</url>
            <title>Stories by Sujit Singh on Medium</title>
            <link>https://medium.com/@sujitsingh_55772?source=rss-1e34b8b45b12------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 23:46:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sujitsingh_55772/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[Lazy loading in React]]></title>
            <link>https://medium.com/@sujitsingh_55772/lazy-loading-in-react-25bb2d6114e1?source=rss-1e34b8b45b12------2</link>
            <guid isPermaLink="false">https://medium.com/p/25bb2d6114e1</guid>
            <category><![CDATA[lazy-loading]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Sujit Singh]]></dc:creator>
            <pubDate>Thu, 30 May 2019 09:33:17 GMT</pubDate>
            <atom:updated>2019-05-31T05:45:58.886Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/683/1*taYxd_kKxzU6Wqig0o_QCQ.png" /></figure><h3>Simple example to illustrate Lazy loading in React</h3><p>React comes with an awesome feature of supporting code splitting and lazy loading using its new features — lazy() and &lt;Suspense&gt;</p><p>We will see how this is implemented and exactly how a chunk can be seen lazily loaded.</p><p>We will first see the <em>non-lazy wa</em>y where we create our root component which is App component inside App.tsx. This will act as container component to dynamically load the modules.</p><p>We will need two more component files — HomeComponent.tsx and TestComponent.tsx. They will simply render a text mentioning component text.</p><p>So our files created so far will be as follows:</p><p><strong>App.tsx:</strong></p><pre>function App(props) {</pre><pre>   return (</pre><pre>      &lt;h1&gt;Welcome to lazy loading in React&lt;/h1&gt;</pre><pre>   )</pre><pre>}</pre><p><strong>Home.tsx:</strong></p><pre>export default function Home() {</pre><pre>   return &lt;h1&gt;Hello HOME&lt;/h1&gt;;</pre><pre>}</pre><p><strong>Test.tsx:</strong></p><pre>export default function Test() {</pre><pre>   return &lt;h1&gt;Hello HOME&lt;/h1&gt;;</pre><pre>}</pre><p>Now, lets put some code to load the Home and Test components based on Route matched:</p><p><strong>App.tsx:</strong></p><pre>import Home from &quot;./Home&quot;;<br>import Test from &quot;./Test&quot;;</pre><pre>function App(props) {<br>  return (</pre><pre>    &lt;h1&gt;Welcome to lazy loading in React&lt;/h1&gt;<br>    &lt;BrowserRouter&gt;<br>        &lt;Switch&gt;<br>            &lt;Route exact path=&quot;/&quot; component={Home} /&gt;<br>            &lt;Route exact path=&quot;/test&quot; component={Test} /&gt;<br>        &lt;/Switch&gt;<br>    &lt;/BrowserRouter&gt;<br>  )<br>}<br></pre><p>We will also need the links to navigate to those paths.</p><p>App.tsx:</p><pre>import Home from &quot;./Home&quot;;<br>import Test from &quot;./Test&quot;;</pre><pre>function App(props) {<br>  return (</pre><pre>    &lt;h1&gt;Welcome to lazy loading in React&lt;/h1&gt;</pre><pre>    &lt;BrowserRouter&gt;<br>        &lt;div&gt;<br>            &lt;Link to=&quot;/&quot;&gt;HOME&lt;/Link&gt;<br>            &lt;Link to=&quot;/test&quot;&gt;TEST&lt;/Link&gt;<br>        &lt;/div&gt;<br>        &lt;Switch&gt;<br>            &lt;Route exact path=&quot;/&quot; component={Home} /&gt;<br>            &lt;Route exact path=&quot;/test&quot; component={Test} /&gt;<br>        &lt;/Switch&gt;<br>    &lt;/BrowserRouter&gt;<br>  )<br>}</pre><p>So this is the non-lazy way of loading the components. Which means, when you run this, and stay on “/” path only, both the components — Home and Test are loaded upfront.</p><p>You can actually find their src in Developer Tool’s Source tab and finding them using ⌘+P and typing “.tsx”. You can see all 3 components loaded as shown below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oY3RLLyBbWaXaSxC9Ld2Cw.png" /></figure><p>Note that here we have not visited Test component view yet.</p><p>User may not visit every part of the application, and thus the application performance can be improved by loading the chunks of the code as and when user actually lands on it. This is the concept of “Lazy Loading”.</p><p>For lazily loading the code, we import it using React’s <strong>lazy</strong> function. It accepts a function which in turn <em>asynchronously</em> imports the component as follwos:</p><pre><em>// Lazily loading the Home component.</em></pre><pre>const Home = React.lazy(() =&gt; import(&quot;./Home&quot;));</pre><p>But while importing the component, being asynchronous, it throws error of component’s rendering being suspended —</p><pre>A React component suspended while rendering, but no fallback UI was specified.</pre><p>This is handled using <strong>&lt;Suspense&gt;</strong></p><p>&lt;Suspense&gt; accepts a <strong>fallback</strong> as prop where we specify the JSX which is to be rendered while component being loaded. The Suspend component surrounds the part which renders lazily loaded components as shown below:</p><pre>&lt;Suspense fallback={&lt;p&gt;Lazy Component loading…&lt;/p&gt;}&gt;<br>...<br><em>// Code to render lazily loaded components<br></em>...<br>&lt;/Suspense&gt;</pre><p>Thats it, our App component now looks as follows:</p><p><strong>App.tsx:</strong></p><pre><em>// import Home from &quot;./Home&quot;;<br>// import Test from &quot;./Test&quot;;</em></pre><pre>const Home = React.lazy(() =&gt; import(“./Home”));<br>const Test = React.lazy(() =&gt; import(“./Test”));</pre><pre>function App(props) {<br>    return (<br>        &lt;div className=”App”&gt;<br>            &lt;h1&gt;Welcome to lazy loading in React&lt;/h1&gt;<br>            &lt;hr/&gt;<br>            &lt;React.Suspense fallback={&lt;h2&gt;Component loading…&lt;/h2&gt;}&gt;<br>                &lt;BrowserRouter&gt;<br>                    &lt;div&gt;<br>                        &lt;Link to=”/”&gt;HOME&lt;/Link&gt;<br>                        &lt;Link to=”/test”&gt;TEST&lt;/Link&gt;<br>                    &lt;/div&gt;<br>                    &lt;Switch&gt;<br>                        &lt;Route exact path=”/” component={Home} /&gt;<br>                        &lt;Route exact path=”/test” component={Test} /&gt;  <br>                    &lt;/Switch&gt;<br>                 &lt;/BrowserRouter&gt;<br>            &lt;/React.Suspense&gt;<br>         &lt;/div&gt;<br>    );<br>}</pre><p>Now, if you run the app and by staying on the Home path itself, check (using the steps specified earlier) which tsx files are loaded, you will find that Test.tsx not been loaded:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dBkExLMVEw7-taf0YkKTNQ.png" /></figure><p>Once you visit the Test link, then the Test.tsx file is loaded and remains in bundle thereafter.</p><p>Full implementation can be found here: <a href="https://codesandbox.io/s/lazyloading-57d8q">https://codesandbox.io/s/lazyloading-57d8q</a></p><p>Hope this article helps you to understand what lazy loading is and how to implement same in React.</p><p>Thanks for reading through.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=25bb2d6114e1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>