Conditional Rendering
React components may need to display different things depending on certain conditions. This is called Conditional Rendering. Conditional rendering allows you to change the output with logical operators. This includes if, if/else, logical AND (&&) operator, and the ternary operator.
if statements
We can use if statements to create conditions. Those conditions then determine the output of our application.
In the example below, we have two functions, Rainy and Sunny. Each one returns a simple <h1> message.
In the App function, we use an if statement and props to render which statement will appear in the browser. The if block starts at line 15, and declares isRainy as true. This will return the message from the Rainy function. When isRainy is not true, it will return the <h1>No rain today!</h1> message instead.
| |
We don’t always need to include the else statement if there are only two conditions.
In fact, we could even compare two different functions with a single if statement.
In the example below, we added a second function, Sunny which will run if isRainy is not true.
When isRainy is false or not true, the Sunny function will run and you will see a different message in the browser.
Currently, we are hardcoding these outcomes. In the next chapter, you will learn about making this functionality more dynamic.
| |
Try it out in this sandbox!
Explore CodeSandbox:
- Click the black left border.
- Click the hamburger menu in the top left corner.
- Select
App.jsto explore.
if/else
What about the else? In the examples above, we are not using else.
You could add an else block to the RainOrShine function. It would have the same behavior and output.
| |
&& statements
The logical && operator is often used with if blocks. It is often used for comparison between expressions. It will be true if both expressions it is comparing are true.
| A | B | Returns |
|---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
We can pair the logical && operator with an if statement if we want to render an element or nothing.
We could update our RainOrShine function to use the logical && like this:
| |
We would need to add the testWord to the App function.
| |
If testWord was not "thunder", then nothing would render.
| |
You could also try using the logical Or || and the logical not ! in your application too.
These will work, but are not as useful as the &&.
Ternary Operator
The ternary operator is a single-line expression of an if/else statement.
Syntax
| |
This would look like the following in JavaScript:
| |
Ternary statements are simplified if/else blocks. We could update our RainOrShine function with a ternary operator like this:
| |
Other applications for Conditional Rendering
Conditional Rendering can also be applied to the styling of elements.
Let’s add some to our RainOrShine function. We will need set the conditional as part of the className.
| |
| |
In the code example above, we had to refactor isRainy. First, we declared isRainy outside of RainOrShine. Remember that React, like JavaScript compiles from top to bottom. We need to declare it before the className in line 11 else it renders as undefined. By declaring isRainy outside of a function, it doesn’t need curly braces. We can declare it as we would in a typical JavaScript application.
In line 11, we placed the ternary statement inside a template literal. The ternary operator is a code block; use the $ escape symbol to ensure it renders like a code block and not a string.`
We placed the styling inside the <div>. The styling will only apply to the <div> with that className.
Check Your Understanding
Given the following code, select the output message.
| |
- Silver Fir is an Evergreen tree
- Sweet Gum is a Deciduous tree
- undefined
Refactor HowToSeason so that it returns a ternary statement
| |
1 2 3 4function HowToSeason(props) { const isBland = props.isBland; return isBland : <AddSalt /> ? <p>It's just right</p>; }1 2 3 4function HowToSeason(props) { const isBland = props.isBland; return isBland ? <p>It's just right</p> : <AddSalt />; }1 2 3function HowToSeason(props) { return isBland ? <p>It's just right</p> : <AddSalt />; }1 2 3 4function HowToSeason(props) { const isBland = props.isBland; return isBland ? <AddSalt /> : <p>It's just right</p>; }
The code should render the “Add some spicy spices!” message. Help debug the issue.
| |
The issue is in line 1. The array is not declared correctly.
The issue is in line 4. Props are used incorrectly.
The issue is in line 6. It should be greater than 0.
The issue is in line 6.
isBlandshould be encased in curly braces.
