<?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: Pritam Shrestha</title>
    <description>The latest articles on DEV Community by Pritam Shrestha (@iheathers).</description>
    <link>https://dev.to/iheathers</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%2F633202%2Fcbdd0f96-9902-4438-9119-f7ff063d0179.png</url>
      <title>DEV Community: Pritam Shrestha</title>
      <link>https://dev.to/iheathers</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/iheathers"/>
    <language>en</language>
    <item>
      <title>React Strict Mode - Good Parts Only</title>
      <dc:creator>Pritam Shrestha</dc:creator>
      <pubDate>Wed, 15 Dec 2021 07:50:54 +0000</pubDate>
      <link>https://dev.to/iheathers/react-strict-mode-good-parts-only-18fj</link>
      <guid>https://dev.to/iheathers/react-strict-mode-good-parts-only-18fj</guid>
      <description>&lt;p&gt;&lt;code&gt;StrictMode&lt;/code&gt; is a tool for highlighting potential problems in an application. Like &lt;code&gt;Fragment&lt;/code&gt;, &lt;code&gt;StrictMode&lt;/code&gt; does not render any visible UI. It activates additional checks and warnings for its descendants.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&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;function&lt;/span&gt; &lt;span class="nx"&gt;ExampleApplication&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Header&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StrictMode&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ComponentOne&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ComponentTwo&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;StrictMode&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Footer&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&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;In the above example, strict mode checks will &lt;em&gt;not&lt;/em&gt; be run against the &lt;code&gt;Header&lt;/code&gt; and &lt;code&gt;Footer&lt;/code&gt; components. However, &lt;code&gt;ComponentOne&lt;/code&gt; and &lt;code&gt;ComponentTwo&lt;/code&gt;, as well as all of their descendants, will have the checks.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;Note:&lt;/strong&gt;&lt;br&gt;
Strict mode checks are run in &lt;code&gt;development&lt;/code&gt; mode &lt;code&gt;only&lt;/code&gt;; &lt;em&gt;they do &lt;code&gt;not&lt;/code&gt; impact the &lt;code&gt;production build&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Strict mode can’t automatically detect side effects for you, but it can help you spot them by making them a little more deterministic. This is done by intentionally double-invoking the following functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class component &lt;code&gt;constructor&lt;/code&gt;, &lt;code&gt;render&lt;/code&gt;, and &lt;code&gt;shouldComponentUpdate&lt;/code&gt; methods&lt;/li&gt;
&lt;li&gt;Class component static &lt;code&gt;getDerivedStateFromProps&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;Function component bodies&lt;/li&gt;
&lt;li&gt;State updater functions (the first argument to &lt;code&gt;setState&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Functions passed to &lt;code&gt;useState&lt;/code&gt;, &lt;code&gt;useMemo&lt;/code&gt;, or &lt;code&gt;useReducer&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By intentionally double-invoking methods like the component constructor, strict mode makes patterns like this easier to spot.&lt;/p&gt;

&lt;p&gt;💡 The double invocation is the reason why we see double logs in the console when we do not expect them at all.&lt;/p&gt;

&lt;p&gt;💡 &lt;strong&gt;Note:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Starting with React 17, React automatically modifies the console methods like &lt;code&gt;console.log()&lt;/code&gt; to silence the logs in the second call to lifecycle functions. (&lt;code&gt;Not the function bodies&lt;/code&gt;) However, it may cause undesired behavior in certain cases where &lt;a href="https://github.com/facebook/react/issues/20090#issuecomment-715927125"&gt;a workaround can be used&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/docs/strict-mode.html"&gt;Strict Mode - React&lt;/a&gt;&lt;/p&gt;

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