<?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: Donovan</title>
    <description>The latest articles on DEV Community by Donovan (@hellodonovan).</description>
    <link>https://dev.to/hellodonovan</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%2F220764%2F498b8878-45cb-45b8-994e-753709cf12c6.png</url>
      <title>DEV Community: Donovan</title>
      <link>https://dev.to/hellodonovan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hellodonovan"/>
    <language>en</language>
    <item>
      <title>React and Vue: Conditional Rendering</title>
      <dc:creator>Donovan</dc:creator>
      <pubDate>Thu, 14 Sep 2023 19:07:33 +0000</pubDate>
      <link>https://dev.to/hellodonovan/react-and-vue-conditional-rendering-3mob</link>
      <guid>https://dev.to/hellodonovan/react-and-vue-conditional-rendering-3mob</guid>
      <description>&lt;p&gt;For as long as I have been trying to get into tech, it's been recommended by a lot of people to learn React. On every YouTube video, Reddit Post and Tweet, I've seen people say React will help you get hired and grow your career. It's around this time in 2020 that Honeypot Dev released it's &lt;a href="https://youtu.be/OrxmtDw4pVI?si=hoV5YcW1fbY9pyX1" rel="noopener noreferrer"&gt;Vue Documentary&lt;/a&gt;  (They've also released a &lt;a href="https://www.youtube.com/watch?v=8pDqJVdNa44" rel="noopener noreferrer"&gt;React doc&lt;/a&gt; earlier this year.) I thought Evan was so cool and loved the passion shown by all the devs. Due to the doc, I wanted to try out Vue myself. I held off and decided to dive heavily into the React ecosystem to be more employable. Fast forward to 2023, and I still have no job and have essentially given up my job search. I still love coding though, so now I'm diving in.&lt;/p&gt;

&lt;p&gt;I wanted to learn how to add functionality with both Vue and React. Rendering elements conditionally is something a framework does easily. However, React and Vue do it differently.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conditional rendering in React.
&lt;/h3&gt;

&lt;p&gt;If you've worked with JavaScript you most likely will be familiar with &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_operator" rel="noopener noreferrer"&gt;ternary operators&lt;/a&gt; and &lt;code&gt;if&lt;/code&gt; statements. Rendering things in React looks like JavaScript, probably because it is JavaScript lol.&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;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isSandwich&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&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="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&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;isSandwich&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;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;A hotdog is a sandwich&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&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;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;NO its not a sando&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&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;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;




&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If your condition is true, in this case isSandwich, &lt;code&gt;&amp;lt;p&amp;gt;Yes the Hotdog is a sandwich&amp;lt;/p&amp;gt;&lt;/code&gt; will be rendered. If not the other &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag after the &lt;code&gt;:&lt;/code&gt; will render.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlegqcetlohsltm0apct.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlegqcetlohsltm0apct.png" alt="Big glizzy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conditional Rendering in Vue
&lt;/h3&gt;

&lt;p&gt;In Vue you have access to &lt;a href="https://vuejs.org/api/built-in-directives.html#v-else-if" rel="noopener noreferrer"&gt;Vue Directives&lt;/a&gt; which are like HTML attributes that allow us to manipulate DOM elements. The ones that are of important to us right now are: &lt;code&gt;v-if&lt;/code&gt;, &lt;code&gt;v-else&lt;/code&gt; and &lt;code&gt;v-show&lt;/code&gt;. They work exactly like they would you would expect.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"isSandwich"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hotdog is a sandwich&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt; &lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;vue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isSandwich&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Vue has an additional directive for this use case called &lt;code&gt;v-show&lt;/code&gt;. &lt;br&gt;
For this example lets make &lt;code&gt;isSandwich&lt;/code&gt; false so it won't be visible.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;p&lt;/span&gt; &lt;span class="na"&gt;v-show=&lt;/span&gt;&lt;span class="s"&gt;"isSandwich"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Hotdog is a sandwich&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;template&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt; &lt;span class="na"&gt;setup&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;vue&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isSandwich&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="k"&gt;script&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag won't be visible, but we should take a look at our developer tools. It looks like that it was actually rendered in our DOM but with a property of &lt;code&gt;display: none&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj4t9252s8urjpq8d5hag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj4t9252s8urjpq8d5hag.png" alt="v-show renders a DOM element with display:none"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So which one should you use? Luckily the Vue docs have a &lt;a href="https://vuejs.org/guide/essentials/conditional.html#v-if-vs-v-show" rel="noopener noreferrer"&gt;section just for this question.&lt;/a&gt; The tldr straight from the docs is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Generally speaking, &lt;code&gt;v-if&lt;/code&gt; has higher toggle costs while &lt;code&gt;v-show&lt;/code&gt; has higher initial render costs. So prefer &lt;code&gt;v-show&lt;/code&gt; if you need to toggle something very often, and prefer &lt;code&gt;v-if&lt;/code&gt; if the condition is unlikely to change at runtime.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Conditionally rendering something in Vue and React makes sense. You might prefer one over the other but, they're both fun to work with for now.&lt;/p&gt;

</description>
      <category>react</category>
      <category>vue</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
