45 lines
846 B
Markdown
45 lines
846 B
Markdown
![]() |
---
|
||
|
title: Render with an If-Else Condition
|
||
|
---
|
||
|
# Render with an If-Else Condition
|
||
|
|
||
|
---
|
||
|
## Solutions
|
||
|
|
||
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
||
|
|
||
|
```jsx
|
||
|
class MyComponent extends React.Component {
|
||
|
constructor(props) {
|
||
|
super(props);
|
||
|
this.state = {
|
||
|
display: true
|
||
|
}
|
||
|
this.toggleDisplay = this.toggleDisplay.bind(this);
|
||
|
}
|
||
|
toggleDisplay() {
|
||
|
this.setState({
|
||
|
display: !this.state.display
|
||
|
});
|
||
|
}
|
||
|
render() {
|
||
|
// change code below this line
|
||
|
if (this.state.display) {
|
||
|
return (
|
||
|
<div>
|
||
|
<button onClick={this.toggleDisplay}>Toggle Display</button>
|
||
|
<h1>Displayed!</h1>
|
||
|
</div>
|
||
|
);
|
||
|
} else {
|
||
|
return (
|
||
|
<div>
|
||
|
<button onClick={this.toggleDisplay}>Toggle Display</button>
|
||
|
</div>
|
||
|
);
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
```
|
||
|
|
||
|
</details>
|