<?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: dcFromNiuMa</title>
    <description>The latest articles on Forem by dcFromNiuMa (@niuma).</description>
    <link>https://forem.com/niuma</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%2F1062146%2F121c49d8-46e9-4208-9e3f-56616e582dc0.png</url>
      <title>Forem: dcFromNiuMa</title>
      <link>https://forem.com/niuma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/niuma"/>
    <language>en</language>
    <item>
      <title>When i used Hooks useEffect with setInterval</title>
      <dc:creator>dcFromNiuMa</dc:creator>
      <pubDate>Mon, 10 Apr 2023 03:46:49 +0000</pubDate>
      <link>https://forem.com/niuma/when-i-used-hooks-useeffect-with-setinterval-244b</link>
      <guid>https://forem.com/niuma/when-i-used-hooks-useeffect-with-setinterval-244b</guid>
      <description>&lt;p&gt;I'm based on the React Docs write a setInterval in the useEffect, &lt;br&gt;
i wants start a timer when app is mounted,&lt;br&gt;
the recommended writing is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [a, setA] = useState(0);
useEffect(() =&amp;gt; {
  const timer = setInterval(() =&amp;gt; {
    setA(x =&amp;gt; x + 1);
  }, 1000);
  return () =&amp;gt; clearInterval(timer);
}, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When there is needs some dependency，i've been advised to use useState's callback function for avoid dependency, but when i needs another state b，it's the condition of a's self-increment.&lt;br&gt;
like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [a, setA] = useState(0);
const [b, setB] = useState(0);
useEffect(() =&amp;gt; {
  const timer = setInterval(() =&amp;gt; {
    setA(x =&amp;gt; {
      if (b &amp;gt; 5) {
        return x;
      }
      return x + 1;
    });
  }, 1000);
  return () =&amp;gt; clearInterval(timer);
}, []); // dependency error, that required dependency b
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To fix this problem, i combined state a and b，like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [c, setC] = useState({
  a: 0,
  b: 0,
});
useEffect(() =&amp;gt; {
  const timer = setInterval(() =&amp;gt; {
    setC(x =&amp;gt; {
      if (x.b &amp;gt; 5) {
        return x;
      }
      return { a: x.a + 1, b: x.b };
    });
  }, 1000);
  return () =&amp;gt; clearInterval(timer);
}, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is this the right way to write it, or should I use useEffectEvent?&lt;/p&gt;

</description>
      <category>react</category>
    </item>
  </channel>
</rss>
