<?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>Forem: Nathanael</title>
    <description>The latest articles on Forem by Nathanael (@human_eiffel).</description>
    <link>https://forem.com/human_eiffel</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%2F1034314%2Fd031908a-e319-451b-aefb-f3eab0bfff38.jpg</url>
      <title>Forem: Nathanael</title>
      <link>https://forem.com/human_eiffel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/human_eiffel"/>
    <language>en</language>
    <item>
      <title>Make Your Website's User Experience Better with React Suspense</title>
      <dc:creator>Nathanael</dc:creator>
      <pubDate>Sun, 02 Apr 2023 19:37:46 +0000</pubDate>
      <link>https://forem.com/human_eiffel/make-your-websites-user-experience-better-with-react-suspense-1c9m</link>
      <guid>https://forem.com/human_eiffel/make-your-websites-user-experience-better-with-react-suspense-1c9m</guid>
      <description>&lt;p&gt;You've probably been in a situation where you visited a website and that site took a bit of time to load, you most likely had to sit down waiting as the loading animation repeated itself. Slow loading speeds are often due to the large amount of code or data the site is loading at once. Having to wait for the site to load could be extremely annoying, not to mention a waste of time.&lt;/p&gt;

&lt;p&gt;Trying to navigate through the website as the website continued to load was probably not the best experience, and you probably had to wait until the browser had loaded all the code necessary for the site to function.&lt;/p&gt;

&lt;p&gt;If you're a React developer, you can massively speed up site load time by indicating an order of render for components using &lt;code&gt;lazy()&lt;/code&gt; and &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  React lazy()
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;lazy()&lt;/code&gt; is a function that relies on dynamic &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import"&gt;import&lt;/a&gt; and reduces your website or web app load time by &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Code_splitting"&gt;code splitting&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Lazy-loading a component tells the browser that the component isn't a priority, the browser will load all other components and load a lazy-loaded component only when a user navigates to that component or route.&lt;/p&gt;

&lt;p&gt;Lazy loading offers several advantages, some of which are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cut down resources needed to load a site&lt;/li&gt;
&lt;li&gt;Reduce the time it takes a user to interact with a site&lt;/li&gt;
&lt;li&gt;Improve a website's user experience&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Is Lazy loading necessary?
&lt;/h2&gt;

&lt;p&gt;We'll answer this using an example. The code snippet below is from a React app with a &lt;code&gt;Navbar&lt;/code&gt; component and two other components named &lt;code&gt;PageOne&lt;/code&gt; and &lt;code&gt;PageTwo&lt;/code&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 PageOne from "./components/PageOne";
import PageTwo from "./components/PageTwo";
import { Routes, Route } from "react-router-dom";
import Navbar from "./components/Navbar";
function App() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;Navbar /&amp;gt;
      &amp;lt;Routes&amp;gt;
        &amp;lt;Route path="/" element={&amp;lt;PageOne /&amp;gt;} /&amp;gt;
        &amp;lt;Route
          path="pagetwo" element={&amp;lt;PageTwo /&amp;gt;}
        /&amp;gt;
      &amp;lt;/Routes&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;PageOne&lt;/code&gt; and &lt;code&gt;PageTwo&lt;/code&gt; components contain two images each.&lt;/p&gt;

&lt;p&gt;On initial load, we land on our index or home page shown in the image below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dT68hHXy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4z463xlqy3w3a07qgklf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dT68hHXy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4z463xlqy3w3a07qgklf.png" alt="Home page of a website showing two adorable cats" width="880" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have not navigated to the Second Page/PageTwo component.&lt;/p&gt;

&lt;p&gt;However, the browser has already loaded all the components in the background. To confirm this, I'll navigate to the Sources tab in the Dev tools of chrome browser(If you use Firefox this tab is called 'debugging')&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QnghERK---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aufcj0rmfqd7zrmp5i78.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QnghERK---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aufcj0rmfqd7zrmp5i78.png" alt="A screenshot of chrome's devtools with a red arrow pointing to the components file tree" width="664" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There's an arrow pointing to the components folder in the file tree of the React App on the left side of the image. You can see the browser has already loaded the PageTwo component even though I haven't navigated there yet. This could be an unnecessary drain of user resources, you might wonder, how?&lt;/p&gt;

&lt;p&gt;Let's assume you're a content creator that has certain content available for paid users only, loading that content in the background for free users even though they can't access it would not make much sense.&lt;/p&gt;

&lt;p&gt;Lazy-loading that premium content will fix this and prevent the browser from loading that content unless a user can access it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started with lazy()
&lt;/h2&gt;

&lt;p&gt;Lazy-loading a component takes a different approach from the traditional import() method. I've modified the App component to demonstrate lazy loading&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { lazy, Suspense} from "react";
import { Routes, Route } from "react-router-dom";
import Navbar from "./components/Navbar";
import PageOne from "./components/PageOne";
const PageTwo = lazy(() =&amp;gt; {import("./components/PageTwo")});

function App() {  
  return (
    &amp;lt;&amp;gt;
      &amp;lt;Navbar /&amp;gt;
      &amp;lt;Routes&amp;gt;
        &amp;lt;Route path="/" element={&amp;lt;PageOne /&amp;gt;} /&amp;gt;
        &amp;lt;Route
          path="pagetwo"
          element={
            &amp;lt;Suspense fallback="Loading..."&amp;gt;
              &amp;lt;PageTwo /&amp;gt;
            &amp;lt;/Suspense&amp;gt;
          }
        /&amp;gt;
      &amp;lt;/Routes&amp;gt;
    &amp;lt;/&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Let's go over the modifications made to the App component&lt;/p&gt;

&lt;p&gt;To lazy load a component, the first thing you need to do is import lazy and Suspense from React and you can see this below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { lazy, Suspense } from 'react'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next step to take is to import your component in the lazy function block and assign it to a variable. You can see this in the example below. The PageTwo component is now being imported in the lazy function block and declared as a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const PageTwo = lazy(() =&amp;gt; {import("./components/PageTwo")});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note - &lt;em&gt;Lazy imports should be done outside a function component and not inside. This means you have to declare them at the top with any other exports&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When you're done importing your lazy component, you need to wrap it in &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;. A lazy-loaded component or route must be wrapped in &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; displays a fallback whenever a user navigates to a component and the component is being loaded. You'll learn more about &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; and &lt;code&gt;fallback&lt;/code&gt; as you read on&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;fallback&lt;/code&gt; in &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; is used to display any React component or text you want to render while your component loads. A &lt;code&gt;fallback&lt;/code&gt; is important as it gives visual feedback that the component is loading.&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;Route
          path="pagetwo"
          element={
            &amp;lt;Suspense fallback="Loading..."&amp;gt;
              &amp;lt;PageTwo /&amp;gt;
            &amp;lt;/Suspense&amp;gt;
          }
        /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're wrapping a route component in &lt;code&gt;Suspense&lt;/code&gt;, it has to be within the curly braces of the &lt;code&gt;Route element&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The route has now been lazy-loaded and after reloading the React App the change is evident in the Dev tools&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rOhnGrxa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tddx46zd2uofb4h37tb5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rOhnGrxa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tddx46zd2uofb4h37tb5.png" alt="A screenshot of chrome's devtools but this time a component has been lazy loaded and isn't loading unnecessarily" width="518" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check it out, the &lt;code&gt;PageTwo&lt;/code&gt; component was not loaded by the browser on initial render. The component will only be loaded when I navigate there. I've included an animated image below to show this in effect.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VLCsQCTn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7lhs3tbj60apsewtd46l.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VLCsQCTn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7lhs3tbj60apsewtd46l.gif" alt="An animated image showing how a component only loads when I navigate there and not when the website is loaded" width="880" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The browser only loaded &lt;code&gt;PageTwo&lt;/code&gt; when I navigated there and not on the initial render as it did before. This is a huge performance boost as the code and files loaded on the initial render are now smaller than before. Now that you've seen how good lazy-loading is, it's time to see what &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; does.&lt;/p&gt;

&lt;h2&gt;
  
  
  React Suspense
&lt;/h2&gt;

&lt;p&gt;The initial explanation of &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; was brief and in this section, you'll learn more about &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;. You'll learn:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;How to use one &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; for multiple components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How to use multiple &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; for different components&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;How to add an &lt;code&gt;ErrorBoundary&lt;/code&gt; to &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;PageTwo&lt;/code&gt; component used in the section discussing lazy loading contains a &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; with a &lt;code&gt;fallback&lt;/code&gt; with the text "Loading..." The image below shows what the suspense displayed while the component was loaded in the background&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--JpSaQV3W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owrxbja04dh835d1p1aq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--JpSaQV3W--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/owrxbja04dh835d1p1aq.png" alt="A screenshot showing a site's homepage with a navbar and a loading text on screen" width="880" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; &lt;code&gt;fallback&lt;/code&gt; would normally be a loader component with a &lt;a href="https://flowbite.com/docs/components/spinner/"&gt;spinner&lt;/a&gt; or a &lt;a href="https://flowbite.com/docs/components/skeleton/"&gt;skeleton&lt;/a&gt; loader, but we'll keep things simple in this project.&lt;/p&gt;

&lt;p&gt;Something worth mentioning is that you can use &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; on components that aren't lazy loaded.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrap multiple elements in one suspense
&lt;/h3&gt;

&lt;p&gt;You can wrap several components in a single &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;. Multiple components wrapped in a &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; will be loaded at the same time with a single &lt;code&gt;fallback&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The code below has three components wrapped in a single &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;LightComponent&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/LightComponent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MediumComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/MediumComponent&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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HeavyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/HeavyComponent&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;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;section&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Loading&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;LightComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MediumComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HeavyComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Suspense&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/section&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are three components of varying render times in the code snippet above, they are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;LightComponent&lt;/code&gt; - Contains a simple text.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;MediumComponent&lt;/code&gt; - Contains an image.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;HeavyComponent&lt;/code&gt; - Contains an image.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All the components are in the same &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; with a &lt;code&gt;fallback&lt;/code&gt;. &lt;code&gt;MediumComponent&lt;/code&gt; and &lt;code&gt;HeavyComponent&lt;/code&gt; are lazy-loaded, You may have noticed that LightComponent isn't lazy-loaded but still works with &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;. As I mentioned, you don't need to lazy load a component for it to work with &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Placing all three components in the same &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; means all three components will be completely loaded before rendering. The result is the image below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_u8NVkBI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfjxtv9s7lpbltt02wb4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_u8NVkBI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfjxtv9s7lpbltt02wb4.png" alt='Image of a black screen with a simple text of "Loading"' width="706" height="363"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One or all of the three components is not ready to render to the screen, this causes the &lt;code&gt;fallback&lt;/code&gt; to be displayed until all components are ready. After fully loading all the components, it renders them all at once&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HUSHf5Tz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u53icraszahdgdml2fna.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HUSHf5Tz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u53icraszahdgdml2fna.png" alt="A homepage of a site showing two adorable cats" width="641" height="633"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see from the above example that wrapping all your elements in one &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; will load all your components at once, even if some of the components are already loaded and ready to render. The page will only display the components when everything is loaded and ready to render.&lt;/p&gt;

&lt;h3&gt;
  
  
  Nesting suspense
&lt;/h3&gt;

&lt;p&gt;You've seen that wrapping several elements in the same &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; might not always be the best option. You can wrap your components in different &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; if you have components with varying sizes.&lt;/p&gt;

&lt;p&gt;Check the code snippet below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;LightComponent&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/LightComponent&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MediumComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/MediumComponent&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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;HeavyComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/HeavyComponent&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;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;section&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Suspense&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Loading Light Component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;LightComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Suspense&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Loading Medium Component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MediumComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Suspense&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Loading Heavy Component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HeavyComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Suspense&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Suspense&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/section&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how each component has its own &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;. Wrapping each component with its &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; means it will now be rendered in order of readiness. The order of render for our App would now be:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The entire &lt;code&gt;App Component&lt;/code&gt; would start loading with a fallback of &lt;code&gt;Loading...&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;LightComponent&lt;/code&gt; will get rendered to the screen immediately after it finishes loading.    &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--X3CpBK-q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/alssaeq6x72zbstf66wb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X3CpBK-q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/alssaeq6x72zbstf66wb.png" alt="Image of a black screen with simple text showing the sequence of loading of components, one components has rendered while the rest are still loading" width="514" height="349"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The fallbacks for &lt;code&gt;MediumComponent&lt;/code&gt; and &lt;code&gt;HeavyComponent&lt;/code&gt; will be displayed as those components continue fetching data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;MediumComponent&lt;/code&gt; will be rendered once fully loaded while the fallback for &lt;code&gt;HeavyComponent&lt;/code&gt; remains as &lt;code&gt;HeavyComponent&lt;/code&gt; keeps fetching data.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i_1W-8lG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yquae4kkxj0u3kpj0cx6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i_1W-8lG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yquae4kkxj0u3kpj0cx6.png" alt="Image of a black screen with simple text showing the sequence of loading of components, two components have rendered but the last one is still loading" width="460" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;HeavyComponent&lt;/code&gt; gets rendered immediately after it fully loads. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--AZNXgjMA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rl1palpmpbph93xqzvdv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--AZNXgjMA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rl1palpmpbph93xqzvdv.png" alt="All images have been fully rendered to the screen" width="641" height="633"&gt;&lt;/a&gt;    &lt;/p&gt;

&lt;p&gt;You've seen how &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; can greatly improve the quality of your websites or web apps. However, just because you can wrap multiple components in &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; does not mean you should. You should wrap only heavy or lazy-loaded components in &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;, Wrapping components with only texts or light images in suspense would not make much sense.&lt;/p&gt;

&lt;p&gt;Some things you can wrap in &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A component that makes a call to an API&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A component with a lot of images&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Routes&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;“Simplicity boils down to two steps: Identify the essential. Eliminate the rest.”&lt;br&gt;&lt;br&gt;
― &lt;strong&gt;Leo Babauta&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Improve Suspense with an Error Boundary
&lt;/h3&gt;

&lt;p&gt;You can level up your code even further by adding an error boundary to &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;. An error boundary catches errors in your code that could otherwise break your app.&lt;/p&gt;

&lt;p&gt;If a component wrapped in &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; without an error boundary throws an error, your app will only display a blank page which wouldn't be very helpful. &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; with an &lt;code&gt;&amp;lt;ErrorBoundary&amp;gt;&lt;/code&gt;, however, will display a fallback instead of a blank page&lt;/p&gt;

&lt;h3&gt;
  
  
  Add an error boundary
&lt;/h3&gt;

&lt;p&gt;To add &lt;code&gt;&amp;lt;ErrorBoundary&amp;gt;&lt;/code&gt; to &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;, you need to install &lt;code&gt;react-error-boundary&lt;/code&gt; as a dependency using either &lt;code&gt;npm&lt;/code&gt; or &lt;code&gt;yarn&lt;/code&gt; install.&lt;/p&gt;

&lt;p&gt;Follow these steps to add &lt;code&gt;&amp;lt;ErrorBoundary&amp;gt;&lt;/code&gt; to &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Navigate to your terminal and type &lt;code&gt;npm install react-error-boundary&lt;/code&gt; if you use the node package manager or &lt;code&gt;yarn add react-error-boundary&lt;/code&gt; if you use yarn.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Import &lt;code&gt;&amp;lt;ErrorBoundary&amp;gt;&lt;/code&gt; into your app at the top&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ErrorBoundary&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-error-boundary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Wrap your suspense in &lt;code&gt;&amp;lt;ErrorBoundary&amp;gt;&lt;/code&gt; and provide a fallback for &lt;code&gt;&amp;lt;ErrorBoundary&amp;gt;&lt;/code&gt;. Using a code snippet from one of our earlier examples to demonstrate this, we have&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ErrorBoundary&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Something&lt;/span&gt; &lt;span class="nx"&gt;went&lt;/span&gt; &lt;span class="nx"&gt;wrong&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&amp;gt;}&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Loading Heavy Component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HeavyComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Suspense&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ErrorBoundary&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;The fallback makes sure you or your users can see an error message instead of a blank page, doing this greatly improves the user experience of your website.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;You can go one step further to improve &lt;code&gt;ErrorBoundary&lt;/code&gt; by adding an &lt;code&gt;ErrorFallback&lt;/code&gt; component instead of a simple text, with an &lt;code&gt;ErrorFallback&lt;/code&gt; component you can display a proper error message, together with a button to refresh the page or reset your app state. You can create an &lt;code&gt;ErrorFallback&lt;/code&gt; by following these steps.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a new React component called &lt;code&gt;ErrorFallback&lt;/code&gt; and in that component add the following code&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ErrorFallback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;resetErrorBoundary&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Something&lt;/span&gt; &lt;span class="nx"&gt;went&lt;/span&gt; &lt;span class="nx"&gt;wrong&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;pre&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/pre&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;resetErrorBoundary&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Try&lt;/span&gt; &lt;span class="nx"&gt;again&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ErrorFallback&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Error Fallback takes in two props:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;error&lt;/code&gt; is the error thrown by the component wrapped in the error boundary.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;resetErrorBoundary&lt;/code&gt; is a function that resets your state or App to its original state before an error was thrown&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Import the &lt;code&gt;ErrorFallback&lt;/code&gt; component in your App component or wherever you used the error boundary&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ErrorFallback&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./components/ErrorFallback&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Replace the &lt;code&gt;fallback&lt;/code&gt; prop in the error boundary with a new prop called &lt;code&gt;FallbackComponent&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ErrorBoundary&lt;/span&gt; &lt;span class="nx"&gt;FallbackComponent&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;ErrorFallback&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;onReset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="cm"&gt;/*add your code to reset state here*/&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Loading Heavy Component&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; 
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HeavyComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt; 
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Suspense&amp;gt;&lt;/span&gt;&lt;span class="err"&gt; 
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/ErrorBoundary&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Add the &lt;code&gt;ErrorFallback&lt;/code&gt; component to the &lt;code&gt;FallbackComponent&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add a function called &lt;code&gt;onReset&lt;/code&gt; to &lt;code&gt;ErrorBoundary&lt;/code&gt; the &lt;code&gt;onReset&lt;/code&gt; function takes in a code used to reset the state of the parent component to its original state before an error was thrown. It's up to you to provide the code&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article you have seen how to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Lazy-load components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; to or Suspend your components.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;fallback&lt;/code&gt; to &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add &lt;code&gt;ErrorBoundary&lt;/code&gt; to &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; to catch any errors.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You've also seen that you need to add &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; to lazy components, but you don't need to lazy load a component before using &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Add &lt;code&gt;lazy()&lt;/code&gt; and &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; to appropriate sections of your code and you'll improve your website's user experience a lot. If you're interested in learning further, you can check the &lt;a href="https://beta.reactjs.org/reference/react/Suspense"&gt;official react docs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have any comments or questions please feel free to ask them or reach out to me on &lt;a href="https://twitter.com/human_eiffel"&gt;Twitter&lt;/a&gt; where I'm most active&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>frontend</category>
      <category>performance</category>
    </item>
    <item>
      <title>Make A Toggle Password Visibility Feature with ReactJS</title>
      <dc:creator>Nathanael</dc:creator>
      <pubDate>Fri, 17 Mar 2023 15:00:00 +0000</pubDate>
      <link>https://forem.com/human_eiffel/make-a-toggle-password-visibility-feature-with-reactjs-3d6m</link>
      <guid>https://forem.com/human_eiffel/make-a-toggle-password-visibility-feature-with-reactjs-3d6m</guid>
      <description>&lt;p&gt;This article will talk about the importance of toggle password visibility feature and how to make it using ReactJS&lt;/p&gt;

&lt;p&gt;Form fields are an important part of our daily activities, we fill them to register as a user on websites, login, send feedback etc. As developers, it is very important to make forms as accessible as possible to everyone. One way to do this is to add a toggle password visibility option to forms.&lt;/p&gt;

&lt;p&gt;A Website or Application that wants its users to have a good experience will have an option to toggle password visibility either using the open-eye/closed-eye icon (like the image below) or using the "show/hide" text.&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%2Fpjrxgfq2txvahs6jr0xe.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%2Fpjrxgfq2txvahs6jr0xe.png" alt="A screenshot of a login form field with a toggle password visibility feature"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Adding a password visibilty toggle feature to password form fields has several advantages, some of which are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allows you to see and correct any typographical errors while typing your password&lt;/li&gt;
&lt;li&gt;Gives you the ability to copy/paste password&lt;/li&gt;
&lt;li&gt;Improves web accessibility for users with some form of disability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are several other reasons why this feature is a must have and I'll link a well-researched article in the conclusion, for now keep reading to see how to make the toggle feature using ReactJS in just three steps (You can skip to the third step if you already have a form and all you need is functionality).&lt;/p&gt;

&lt;p&gt;Note - I assume you have some basic knowledge of ReactJS including hooks, especially the &lt;code&gt;useState&lt;/code&gt; Hook that we'll use. If you're new and have no idea what I'm talking about, feel free to &lt;a href="https://react.dev/reference/react/useState#usestate" rel="noopener noreferrer"&gt;read the docs&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a Login Form
&lt;/h2&gt;

&lt;p&gt;I’ll be using an empty App component for this tutorial, however, feel free to fit this tutorial to your needs.&lt;/p&gt;

&lt;p&gt;Create a &lt;code&gt;form&lt;/code&gt; inside the parent &lt;code&gt;div&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;form&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/div&amp;gt;
  );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create an &lt;code&gt;input&lt;/code&gt; with a type of ‘email’ inside the &lt;code&gt;form&lt;/code&gt;. You can add a heading to the form if you want&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;form&amp;gt;
        &amp;lt;h1&amp;gt;Login&amp;lt;/h1&amp;gt;
        &amp;lt;input
          type="email"
          className=""
          placeholder="Email"
          autoComplete="email"
          required
        /&amp;gt;
 &amp;lt;/form&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;Create a &lt;code&gt;div&lt;/code&gt; below &lt;code&gt;input&lt;/code&gt;. Give the &lt;code&gt;div&lt;/code&gt; a classname of “password-div” and nest two things inside the &lt;code&gt;div&lt;/code&gt;- An &lt;code&gt;input&lt;/code&gt; with type of ‘password’ and a &lt;code&gt;p&lt;/code&gt; tag&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;div className="password-div"&amp;gt;
           &amp;lt;input
             type="password"
             className=""
             placeholder="Password"
             autoComplete="password"
             required
           /&amp;gt;
           &amp;lt;p&amp;gt;Show&amp;lt;/p&amp;gt;
           &amp;lt;/div&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;You can add a &lt;code&gt;button&lt;/code&gt; to the &lt;code&gt;form&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return (

    &amp;lt;div className="App"&amp;gt;
      &amp;lt;form&amp;gt;
        &amp;lt;input
          type="email"
          className=""
          placeholder="Email"
          autoComplete="email"
          required
        /&amp;gt;
        &amp;lt;div className="password-div"&amp;gt;
          &amp;lt;input
            type="password"
            className=""
            placeholder="Password"
            autoComplete="password"
            required
          /&amp;gt;
          &amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;button&amp;gt;Login&amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Style the Login form
&lt;/h2&gt;

&lt;p&gt;You can style the &lt;code&gt;form&lt;/code&gt; yourself or use the CSS snippet below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.App{
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
form {
  background-color: grey;
  padding: 1rem;
  height: 25rem;
  width: 25rem;
}
input{
  width: 100%;
  height: 3rem;
  font-size: 1rem;
  margin: 5px 0;
  text-indent: .5rem;
}
button{
  width: 100%;
  height: 3rem;
  font-size: 1rem;
  background-color: white;
  border: none;
  margin-top: 3rem;
  cursor: pointer;
}
.password-div{
  position: relative;
}
p{
  position: absolute;
  top:20%;
  right: 0;
  font-size: .8rem;
  cursor: pointer;
  color: blue;
}

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

&lt;/div&gt;



&lt;p&gt;Adding the CSS snippet will give you the image below&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%2Feu59ts5rkwo7aujp41hp.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%2Feu59ts5rkwo7aujp41hp.png" alt="A screenshot of a form with two fields which are: Email and Password"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Add functionality to the password toggle
&lt;/h2&gt;

&lt;p&gt;You can make the toggle functional by following these steps:&lt;/p&gt;

&lt;p&gt;Import useState into your component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from "react";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initialize a state called visible and set its default value to false&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [visible, setVisible] = useState(false);

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

&lt;/div&gt;



&lt;p&gt;Create a function called &lt;code&gt;togglePassword&lt;/code&gt; and inside the function block- setVisible to the opposite state of visible i.e. if useState(false) set it to true and vice versa&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const togglePassword = () =&amp;gt; {
    setVisible(!visible);
  };

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

&lt;/div&gt;



&lt;p&gt;Make a small modification to the type prop in the password input. Replace &lt;code&gt;type='password'&lt;/code&gt; with &lt;code&gt;type={visible ? 'text' : 'password'}&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This line of code will change the type of &lt;code&gt;input&lt;/code&gt; depending on the state of visible&lt;/p&gt;

&lt;p&gt;Finally, add the &lt;code&gt;togglePassword&lt;/code&gt; function to the &lt;code&gt;onClick&lt;/code&gt; property of &lt;code&gt;p&lt;/code&gt; and conditionally render the text content based on useState&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;p onClick={togglePassword}&amp;gt;{visible ? 'Hide' : 'Show'}&amp;lt;/p&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You have now added functionality to the password visibility toggle and your form field is more accessible to several people across the globe. &lt;/p&gt;

&lt;p&gt;You can see the results of this article in the animated image below.&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%2Fa9bv4itzw6hh76rixwmi.gif" 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%2Fa9bv4itzw6hh76rixwmi.gif" alt="An animated image of a form with two fields which are: Email and Password, the password visibility is being toggled by a show/hide feature"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Conclusion&lt;br&gt;
You’ve seen how important the password visibility toggle feature is in web development as well as the benefits of having it in your project.&lt;/p&gt;

&lt;p&gt;This article has also shown how to make it yourself. If you're interested in reading more about making your form fields accessible, Nicolas Steenhout talks about it in &lt;a href="https://incl.ca/show-hide-password-accessibility-and-password-hints-tutorial/" rel="noopener noreferrer"&gt;his article&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have any questions or suggestions feel free to drop them in the comments or &lt;a href="https://twitter.com/human_eiffel" rel="noopener noreferrer"&gt;reach out to me on twitter&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>frontend</category>
      <category>ux</category>
      <category>a11y</category>
    </item>
    <item>
      <title>Code debugging for beginners</title>
      <dc:creator>Nathanael</dc:creator>
      <pubDate>Fri, 03 Mar 2023 00:00:00 +0000</pubDate>
      <link>https://forem.com/human_eiffel/code-debugging-for-beginners-12mn</link>
      <guid>https://forem.com/human_eiffel/code-debugging-for-beginners-12mn</guid>
      <description>&lt;h2&gt;
  
  
  What is debugging
&lt;/h2&gt;

&lt;p&gt;If you're a software developer, you've probably experienced situations where your code didn't work as you expected it to, your next step was probably to go through your code looking for the error(s) or to ask someone to help you look for any error(s). The moment you found the error/mistake, you fixed it and let out a sigh of relief as your code now worked correctly.&lt;/p&gt;

&lt;p&gt;That process of checking your code, finding the error and fixing it is called debugging.&lt;/p&gt;

&lt;p&gt;Debugging is one of the most important skills for programmers because it helps them find problems and fix them before a user interacts with their program.&lt;/p&gt;

&lt;h2&gt;
  
  
  History of the term "debugging"
&lt;/h2&gt;

&lt;p&gt;The term "debugging" as used in computer programming has an interesting origin. The popular belief is that it started in the 1940s when a moth got into a computer belonging to Harvard University and crashed it. &lt;a href="https://en.wikipedia.org/wiki/Grace_Hopper"&gt;Admiral Grace Hopper&lt;/a&gt; called the process of removing and fixing the computers "debugging". The term took off from there and became widely accepted by the computer science community soon after.&lt;/p&gt;

&lt;h2&gt;
  
  
  Do you need to know how to debug?
&lt;/h2&gt;

&lt;p&gt;You do! As developers, we interact with a computer using programming languages. We use these to edit information, receive information, send it somewhere else etc. The computer carries out our activities using electronic pulses that convert our information to &lt;a href="https://www.computerhope.com/jargon/b/binary.htm"&gt;binary&lt;/a&gt; figures of 0s and 1s.&lt;/p&gt;

&lt;p&gt;Our constant actions of editing, sending and receiving information means sometimes we can make mistakes and our computers just keep working based on our mistakes.&lt;/p&gt;

&lt;p&gt;The fact that we're prone to making mistakes means we should learn how to resolve these mistakes as efficiently as possible whenever we encounter them.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“If debugging is the process of removing software bugs, then programming must be the process of putting them in.”&lt;/p&gt;

&lt;p&gt;― &lt;strong&gt;Edsger W. Dijkstra&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Common debugging practices
&lt;/h2&gt;

&lt;p&gt;We already saw earlier that debugging is the process of looking through malfunctioning code and fixing errors.&lt;/p&gt;

&lt;p&gt;There are several tools that can help you debug your code, but it is important to learn some basic practices that could help you especially if you're working on a small project or have no experience with debugging tools. Some common debugging practices are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Checking your code for spelling errors or unintentional omissions&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Reading documentation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Checking your console or IDE for error messages&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Googling error messages&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Asking for help&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's talk about these practices briefly and see what they involve.&lt;/p&gt;

&lt;h3&gt;
  
  
  Look through your code
&lt;/h3&gt;

&lt;p&gt;The first step in debugging is to look for inconsistencies in your code. Check for mistakes in your spelling or logic.&lt;/p&gt;

&lt;p&gt;Here's an example, a few days ago I was writing some React code and faced a bug, I copied code from another file and pasted it in the function block shown below. This made my code look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//My Code&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;p&gt;Instead of this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//My Code&lt;/span&gt;
&lt;span class="p"&gt;},[])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This simple mistake gave me a couple of minutes of frustration but I debugged it by looking through my code for inconsistencies.&lt;/p&gt;

&lt;p&gt;A bug does not have to be complicated blocks of code, it can be as simple as omitting a letter while writing code, for example, the code &lt;code&gt;consol.log('Nathanael says Hi')&lt;/code&gt; is meant to log a message on your console but there's a spelling error in the code, this prevents our code from working properly, looking through our code for spelling errors can help us catch this and we simply fix that error by correctly typing &lt;code&gt;console.log('Nathanael says Hi')&lt;/code&gt; .&lt;/p&gt;

&lt;h3&gt;
  
  
  Reading Documentation
&lt;/h3&gt;

&lt;p&gt;One of the most popular jokes in the tech world is "Several hours of debugging can save you from a few minutes of reading documentation". Please take this statement just as it is, a joke. Save yourself from unnecessary pain by thoroughly reading the documentation of whatever you're making use of, even if you're used to working with something similar.&lt;/p&gt;

&lt;p&gt;Reading documentation can feel stressful sometimes because it contains a lot of info, this is understandable, but remember that official documentation contains a lot of info because it is written to be as detailed as possible to help people understand a tool or programming language better and faster.&lt;/p&gt;

&lt;p&gt;Don't save yourself a few minutes of reading documentation, save yourself hours of unnecessary debugging.&lt;/p&gt;

&lt;h3&gt;
  
  
  Check your IDE/Console for errors
&lt;/h3&gt;

&lt;p&gt;Every standard &lt;a href="https://en.wikipedia.org/wiki/Integrated_development_environment"&gt;IDE&lt;/a&gt; has a way of pointing out errors in code to you either visually or using error codes.&lt;/p&gt;

&lt;p&gt;For example there's a mistake in the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;cuisine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Card&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Link&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/recipe/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;              &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h4&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h4&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Link&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;          &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Card&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We've defined the wrong name in our key prop, it should be &lt;code&gt;item.id&lt;/code&gt; not &lt;code&gt;object.id&lt;/code&gt; since we've named our argument 'item'. My IDE instantly catches this and alerts me to it in the image below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZnLZYtUe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1675290442482/25647f98-b8dc-4806-b527-1720e7905507.png%2520align%3D%2522center%2522" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZnLZYtUe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1675290442482/25647f98-b8dc-4806-b527-1720e7905507.png%2520align%3D%2522center%2522" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;My IDE tells me exactly what the problem is and the exact line of code it appears. It says 'object is not defined' and it says the code is on line 22, this allows me see the error and locate it in my code. A lot of IDEs have this capability and this makes them a helpful tool for debugging your code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Googling Error messages
&lt;/h3&gt;

&lt;p&gt;Sometimes, you will run into errors that seem beyond your scope, especially if your IDE gives a vague error message such as a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status"&gt;status code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's see an example of this, let's say you're working with an API and suddenly it stops working, refusing to give data! Let's assume you check your console and see a status code of 402, simply pasting that error code into a google search bar gives an answer to the problem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--yVhtWTNz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1675371300567/21e185ff-b011-4578-84c7-11f3f38fbe3b.png%2520align%3D%2522center%2522" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yVhtWTNz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1675371300567/21e185ff-b011-4578-84c7-11f3f38fbe3b.png%2520align%3D%2522center%2522" alt="" width="" height=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Simply pasting that error code in our search bar has told us that we need to pay to use that API we're trying to work with.&lt;/p&gt;

&lt;p&gt;You can paste almost any error message into Google's search bar and you will likely get your answers in no time.&lt;/p&gt;

&lt;p&gt;Sometimes asking google isn't enough and your questions are left unanswered, what do you do in such a situation? That's where the next point comes in.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ask for help
&lt;/h3&gt;

&lt;p&gt;If you run into an error you can't fix on your own and you have a friend who's familiar with the tool or programming language you're using, asking that friend for help could do you a lot of good, sometimes the best debugger is a friend ready to help.&lt;/p&gt;

&lt;p&gt;What if you don't have developer friends?&lt;/p&gt;

&lt;p&gt;There are several developer communities that are always ready to help, there's a very good chance a lot of developers in the communities have faced what you're facing and have the solutions you need. Some developer communities are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://stackoverflow.com/"&gt;Stack Overflow&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.freecodecamp.org/"&gt;FreeCodeCamp&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://discourse.mozilla.org/c/developer-community/215"&gt;Mozilla Dev Community&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can also find developer communities on social media such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.reddit.com/"&gt;Reddit&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://twitter.com/login?lang=en"&gt;Twitter&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://discord.com/"&gt;Discord&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These lists are by no means exhaustive, they are just a few of many helpful communities out there to get help. All you have to do is join one and politely ask for help. Joining a community is beneficial because you can get more detailed answers and explanations to your errors plus the added bonus of meeting cool people to learn from!&lt;/p&gt;

&lt;h3&gt;
  
  
  Use console.log() or some logging syntax to catch errors
&lt;/h3&gt;

&lt;p&gt;A bonus tip for debugging is to log certain pieces of your code into the console or IDE. For example If you get data or code from somewhere else for example if you call an API and you're not sure what it contains, you can save yourself some headache by console logging the data and seeing what it contains.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Debugging is one of many skills that programmers need. It can be challenging, but with practice it becomes second nature.&lt;/p&gt;

&lt;p&gt;We've seen a few steps you can take to debug better and also places to get help if you get stuck. Hopefully you learned something that'll help you become a better debugger.&lt;/p&gt;

&lt;p&gt;If you want a more in-depth article as well as some solid programming principles, check out &lt;a href="https://www.freecodecamp.org/news/what-is-debugging-how-to-debug-code/"&gt;FreeCodeCamp's article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you have questions or comments be sure to drop them below or text me on &lt;a href="https://twitter.com/human_eiffel"&gt;twitter&lt;/a&gt; where I'm usually active.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>webdev</category>
      <category>programming</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
