<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bodhisattwa Basu</title>
    <description>The latest articles on DEV Community by Bodhisattwa Basu (@bodhi132).</description>
    <link>https://dev.to/bodhi132</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%2F1051930%2Fe1c9d8ad-b80a-4da9-ac37-06f743e5efe0.jpeg</url>
      <title>DEV Community: Bodhisattwa Basu</title>
      <link>https://dev.to/bodhi132</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bodhi132"/>
    <language>en</language>
    <item>
      <title>Understanding Recursion in React</title>
      <dc:creator>Bodhisattwa Basu</dc:creator>
      <pubDate>Fri, 22 Dec 2023 04:26:30 +0000</pubDate>
      <link>https://dev.to/bodhi132/understanding-recursion-in-react-2fll</link>
      <guid>https://dev.to/bodhi132/understanding-recursion-in-react-2fll</guid>
      <description>&lt;p&gt;Before implementing recursion in React Js we need to understand what recursion actually is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Recursion ?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Recursion is the process in which a function calls itself several times until the base case in reached. Let's understand it with an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function count(n) {
    if(n &amp;gt; 5) {
        return;
    }
    n++;
}

count(1);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code the count function is calling itself 5 times until the base case is reached which is (n&amp;gt;5).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why use recursion in React ?&lt;/strong&gt;&lt;br&gt;
Recursion can help make the code more readable and modular. Let's see how with an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const data = [
    {
      name: "Parent",
      children: [
        {
          name: "Child-1",
        },
      ],
    },
    {

      name: "Parent",
      children: [
        {

          name: "Sub-Parent",
          children: [
            {

              name: "Child",
              children: [
                {
                  name: "Sub-Parent",
                },
              ],
            },
          ],
        },
        {
          name: "Child",
        },
      ],
    },
  ];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above is an nested json object. Suppose we have to render each children for that we need to map through each parent and check whether it has an children or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const NonRecursiveComp = ({data}) =&amp;gt; {
    return (
        &amp;lt;div&amp;gt;
            {data.map((parent) =&amp;gt; {
                return (
                    &amp;lt;div key={parent.name}&amp;gt;
                        &amp;lt;span&amp;gt;{parent.name}&amp;lt;/span&amp;gt;
                        {parent.children.map((child) =&amp;gt; {
                            return (
                                &amp;lt;div key={child.name} style={{ paddingLeft: "20px" }}&amp;gt;
                                    &amp;lt;span&amp;gt;{child.name}&amp;lt;/span&amp;gt;
                                    {child.children !== null &amp;amp;&amp;amp; child.children !== undefined &amp;amp;&amp;amp;
                                        child.children.map((subChild) =&amp;gt; {
                                            return (
                                                &amp;lt;div
                                                    key={subChild.name}
                                                    style={{ paddingLeft: "20px" }}
                                                &amp;gt;
                                                    &amp;lt;span&amp;gt;{subChild.name}&amp;lt;/span&amp;gt;
                                                    {subChild.children &amp;amp;&amp;amp;
                                                        subChild.children.map((subChild) =&amp;gt; {
                                                            return (
                                                                &amp;lt;div
                                                                    key={subChild.name}
                                                                    style={{ paddingLeft: "20px" }}
                                                                &amp;gt;
                                                                    &amp;lt;span&amp;gt;{subChild.name}&amp;lt;/span&amp;gt;
                                                                &amp;lt;/div&amp;gt;
                                                            );
                                                        })}
                                                &amp;lt;/div&amp;gt;
                                            );
                                        })}
                                &amp;lt;/div&amp;gt;
                            );
                        })}
                    &amp;lt;/div&amp;gt;
                );
            })}
        &amp;lt;/div&amp;gt;
    )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code we are using map function to render each child element. However the code is getting repeated for each nested data. If the nesting gets more complex the code will be much more longer which will make it difficult to read and maintain. To solve this problem we will use a recursive component to render the data.&lt;/p&gt;

&lt;p&gt;Let's see how recursive component can make the above react code simpler with an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const RecursiveComp = ({data}) =&amp;gt; {
  return (
    &amp;lt;div &amp;gt;
      {data.map((parent) =&amp;gt; {
        return (
          &amp;lt;div key={parent.name}&amp;gt;
            {parent.name &amp;amp;&amp;amp; &amp;lt;div&amp;gt;{parent.name}&amp;lt;/div&amp;gt;}
            &amp;lt;div&amp;gt;
              {parent.children !==null  &amp;amp;&amp;amp; parent.children !== undefined &amp;amp;&amp;amp; &amp;lt;RecursiveComp data={parent.children} /&amp;gt;}
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
        );
      })}
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code we are rendering the component till the base case is reached which is &lt;strong&gt;parent.children !==null  &amp;amp;&amp;amp; parent.children !== undefined&lt;/strong&gt;. As we can see the code is now much simpler and readable.&lt;/p&gt;

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

&lt;p&gt;Recursion is used to break down a problem into simpler parts and can be used for variety of problems. Recursion uses stack memory and is slower than looping so it might not always be the most efficient way to solve a problem. So it is important to figure out if recursion is the right approach for solving a problem before implementing it in your react code base.&lt;/p&gt;

</description>
      <category>react</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
