Files

40 lines
994 B
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Render with an If/Else Condition
---
# Render with an If/Else Condition
2018-10-12 15:37:13 -04:00
---
## Problem Explanation
Inside of the render method of the component, write if/else statements that each have its own return method that has different JSX. This gives programmers the ability to render different UI according to various conditions.
2018-10-12 15:37:13 -04:00
First, wrap the current return method inside of an if statement and set the condition to check if the variable 'display' is true. Remember, you access state using `this.state`.
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```jsx
2018-10-12 15:37:13 -04:00
if (this.state.display === true) {
return (
<div>
<button onClick={this.toggleDisplay}>Toggle Display</button>
<h1>Displayed!</h1>
</div>
);
}
```
Next, create an else statement that returns the same JSX **without** the `h1` element.
```jsx
2018-10-12 15:37:13 -04:00
else {
return (
<div>
<button onClick={this.toggleDisplay}>Toggle Display</button>
</div>
)
}
```
</details>