title: Infinite Loop React Table
body: <p>I have a snippet that simulates a scenario that is causing an infinite loop in a Child component:</p>
  <pre><code>    import React, { useState, useEffect } from 'react';
      import &quot;./styles.css&quot;;
  
      const data = ['one', 'two', 'three'];
  
      //New component with some other stuffs
      function Detail({row}) {
        console.log('in');
        return (&lt;span&gt;You selected {row}&lt;/span&gt;)
      }
  
      export default function App() {
        const [rows, setRows] = useState([]);
        const [selectedRow, setSelectedRow] = useState();
  
        useEffect(() =&gt; { 
          if (rows.length === 0) {
            //get data from service...
            setRows(data);
          }
        }, [rows]);
  
        return (
          &lt;&gt;
            &lt;table&gt;
              &lt;thead&gt;
                &lt;tr&gt;
                    &lt;th&gt;
                      Title
                    &lt;/th&gt;
                &lt;/tr&gt;
              &lt;/thead&gt;
              &lt;tbody&gt;
                {
                  data.map(row =&gt; (
                    &lt;tr key={row}&gt;
                      &lt;td&gt;
                      &lt;button type=&quot;button&quot; onClick={()=&gt; { setSelectedRow(row)}}&gt; {row} &lt;/button&gt;
                      &lt;/td&gt;
                    &lt;/tr&gt;
                  ))
                }
              &lt;/tbody&gt;
            &lt;/table&gt;
            {selectedRow &amp;&amp; (&lt;Detail row={selectedRow}/&gt;)}
          &lt;/&gt;
        );
      }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/RA5MI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RA5MI.png" alt="Code Sanbox output" /></a></p>
  <p><a href="https://codesandbox.io/s/jovial-rain-u9y2o?file=/src/App.js" rel="nofollow noreferrer">CodeSandbox Link</a></p>
  <p>Codesandbox shows just a couple of warnings, but what's happening in my application is that the child component is being rendered infinite times.</p>
  <p>I would appreciate any suggestion.</p>
  <p>Thanks.</p>