<?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: Sune Quist</title>
    <description>The latest articles on DEV Community by Sune Quist (@sunesookhan).</description>
    <link>https://dev.to/sunesookhan</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%2F514428%2F6579cc82-77b9-49ff-b68f-94a9f7142292.png</url>
      <title>DEV Community: Sune Quist</title>
      <link>https://dev.to/sunesookhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sunesookhan"/>
    <language>en</language>
    <item>
      <title>Using [] or null in useState?</title>
      <dc:creator>Sune Quist</dc:creator>
      <pubDate>Tue, 28 Jun 2022 18:34:46 +0000</pubDate>
      <link>https://dev.to/sunesookhan/using-or-null-in-usestate-5da0</link>
      <guid>https://dev.to/sunesookhan/using-or-null-in-usestate-5da0</guid>
      <description>&lt;p&gt;In React I often debate with myself whether to use null or an empty array when making a useState, which I know later on will contain data.&lt;/p&gt;

&lt;p&gt;Well of course this is either opinion-based, or it may be a set rule in your company. That is why after I have described my reason for almost always using null, I'd like to hear what you do?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My reason&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Anyway, the reason I tend to go for null, is that when it comes to conditional statements in the HTML, it is much easier to simply have it look for the data to either be there or not. &lt;/p&gt;

&lt;p&gt;Of course, this won't ensure that a condition for it being an array is in place, but rather if you are sure the content you will put out is always guaranteed to be an array, then there is no reason to overuse conditionals.&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setNewArray&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&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="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Fragment&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
   &lt;span class="si"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;newArray&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="c1"&gt;// loop...&lt;/span&gt;
   &lt;span class="si"&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;Fragment&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Now, what do you do in your company, or do you even use states?&lt;/strong&gt;&lt;/p&gt;

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