Fix Seeing “0” in Your JSX Code

By  on  

The early days of the web felt like the wild west when it came to coding practices -- just make it work. Then we became enlightened to better practices, separating HTML from CSS and JavaScript. Then came React and JSX, where we combine JavaScript, HTML, and even CSS with Styled Components -- what an elegant mess we've made!

Every once in a while part of that mess is me seeing 0 displaying in the output of my JSX code, and I'm reminded why: improper handling of variable typing, combined with using &&. Let me explain!

One of the popular patterns in JSX is:

<div>Some header</div>
{someValue && <div>Some header</div>}

The pattern makes sense but check out the difference in outputs between string and number types:

"0" && "Thing"
> "Thing"
0 && "Thing"
> 0

Note that a string value of 0 allows the second value to be returned, but a number typed 0 simply returns the 0. The best practice is always to cast the value to a Boolean in your JSX:

{Boolean(value) && ....}

Typescript and even PropTypes can help to catch these issues but even seasoned veterans sometimes hit these pain points.

Recent Features

  • By
    How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

  • By
    Send Text Messages with PHP

    Kids these days, I tell ya.  All they care about is the technology.  The video games.  The bottled water.  Oh, and the texting, always the texting.  Back in my day, all we had was...OK, I had all of these things too.  But I still don't get...

Incredible Demos

  • By
    Digg-Style Dynamic Share Widget Using MooTools

    I've always seen Digg as a very progressive website. Digg uses experimental, ajaxified methods for comments and mission-critical functions. One nice touch Digg has added to their website is their hover share widget. Here's how to implement that functionality on your site...

  • By
    Custom Scrollbars in WebKit

    Before each of the browser vendors we like was providing unique CSS controls, Internet Explorer was setting the tone.  One such example is IE's early implementation of CSS filters. Internet Explorer was also the first browser that allowed developers to, for better or worse, customize...

Discussion

  1. Cuong

    You also can use {!!value && .... }

  2. Marko

    I usually like to be more explicit with these checks to make them more clear, so in this case I would maybe go for this:

        {value == 0 && ...}
    

    Even though === strict equality usually is better and I prefer it, but for this case I would say it’s ok.

    But that !!value mentioned by Cuong is also really good approach. It can just trip up less experienced people.
    One pattern that I also avoid is using myArray.length && and I like to be explicit like myArray.length > 0 && since it makes it more obvious what is going on here. It also can avoid these subtle pitfalls.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!