The original post lives here
Conditional rendering with React is just a matter of deciding which element object to return. You can use if and switch statements, the conditional operator, and even the && operator! This gives you a lot of power — I’ll go into the details next week. The problem is that all of this power makes it easy to shoot yourself in the foot. Lucky for you, getting familiar with these 3 anti-patterns will help help you avoid most of the pain. I’ll start with the issue I run into most…
         someNumber && ...     
      Let’s do a little quiz on the && operator: what is the value of x?       let x = 0 && \"1\"       You can check your answer below:      The value of x is 0       There’s something special about 0: it’s the only falsy value that JSX renders as text.      This makes it generally a bad thing to guard on it.      For example, say you’re printing verses of “bottles of beer”, but you don’t want to print      the last verse with “0” bottles of beer. You might attempt to handle this by prefixing      the verse with bottlesOfBeer &&:                   import React from 'react'         import ReactDOM from 'react-dom'          let bottlesOfBeer = 0          ReactDOM.render(         bottlesOfBeer &&                      Take one down and pass it around,              {bottlesOfBeer} bottles of beer on the wall.         
,         document.getElementById('root')         )     One trick to avoid this is to coerce the left-hand side of your && operator into a boolean by using the !! operator. To see this in action, try removing the !! operator from the below example.
         import React from 'react'         import ReactDOM from 'react-dom'          let bottlesOfBeer = 0          ReactDOM.render(                      {              !!bottlesOfBeer &&             {bottlesOfBeer} bottles of beer on the wall.
             }         ,         document.getElementById('root')         )     I heard you like Conditional operators so I put a Conditional operator in your Conditional operator
One of the things about conditional operators, is that you can put them in other conditional operators. This is a little hard to explain, so the best way to see why this is a bad idea is to look at this example.         const Button = props =>         props.status === 'busy' ?             } /> :         props.status === 'error' ?             } /> :         props.status === 'disabled ?              :              If, Only, Else, etc…
You might wonder why nobody has created an Only component that can be used to simplify conditional rendering.                      Hi, {user.name}!
              React.createElement(); they’ll be evaluated regardless of the value of the when prop! If you’re not convinced, click “compiled” and take a look.      In contrast, if you were to implement the same component using an && guard, then the h1 tag will not be evaluated unless user has a value.      Try confirming this for yourself by changing user to null in the example below:               import React from 'react'         import ReactDOM from 'react-dom'          function WhatAHangover(props) {         let user = props.user         return user && Good morning, {user.name}.
         }          ReactDOM.render(                                   After sleeping on it I feel like maybe I can do that.
         ,         document.getElementById('root')         )      
 
              

 
    
Top comments (0)