Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Conditionally Render A React Component


    In this lesson, we will explore JSX a little further by looking at how to conditionally render JSX with a Javascript ternary operator. We will see how JSX is simply syntax sugar on top of the React API: React.createElement.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: Here, we have a handy message component that renders a message inside of a div. What if we don't actually pass a message at all? What if that message were to be null, for example? That's not going to render anything.

    In our case, we want to have it render no message. What we're going to do here is we'll say if there is not a message, then we'll return a div with no message. Otherwise, we'll return that message. Now, we can have hi there. That'll render hi there, and then we can also have null to render no message.

    Now, the key takeaway here is that JSX is simply an abstraction on top of React.createElement We'll say React.createElement, div, null for the props, and then no message will be a string for the children. We'll do the same thing here with React.createElement with a div, null, and then the message.

    This is functionally equivalent to what we had before. This is just what the JSX would be transpiled down to. With it like this, we can see that this is really just JavaScript. Instead of this if statement, we could do a ternary where we'd say return message?, and then if there is a message, then we'll do React.createElement with that message.

    Otherwise, we'll do React.createElement with no message. Then we can remove all of this, and that's functionally equivalent as well. We can put a message in here, and that works also.

    If we wanted to wrap all of this inside of yet another div, then what we can do is we'll take this. I'm going to cut it out, and we'll make a div. Then we have to interpolate our JavaScript syntax into here. That is functionally equivalent, except now we're wrapping everything inside of a div.

    When you start into JSX, you need to open up some interpolation to do any of the JavaScript stuff that you see in here. This is why very often in React code in the render method, you'll see the use of ternaries, because the curly braces must accept an expression in there.

    That's why you'll see ternaries, because you can't have an if statement in here. That doesn't work in there. Using ternaries to conditionally render different components is a really nice way to compose these components together.

    Let's go ahead and refactor this back to JSX, because it looks a little bit nicer. I'll say div with message, and then div with no message. Then we can change this to null to get our no message.