Created
November 22, 2020 00:53
-
-
Save hadesunseenn/71d96adf937a8d676d0ce74a296a3c85 to your computer and use it in GitHub Desktop.
React use if else condition in render function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import './App.css'; | |
class App extends Component { | |
state = { | |
showPersons: false | |
} | |
togglePersonsHandler = () => { | |
const doesShow = this.state.showPersons; | |
this.setState( { showPersons: !doesShow } ); | |
} | |
render () { | |
const style = { | |
backgroundColor: 'white', | |
font: 'inherit', | |
border: '1px solid blue', | |
padding: '8px', | |
cursor: 'pointer' | |
}; | |
let persons = null; | |
if ( this.state.showPersons ) { | |
persons = ( | |
<div> | |
hide me | |
</div> | |
); | |
} | |
return ( | |
<div className="App"> | |
<h1>Hi, I'm a React App</h1> | |
<p>This is really working</p> | |
<button | |
style={style} | |
onClick={this.togglePersonsHandler}>Toggle Persons</button> | |
{persons} | |
</div> | |
); | |
} | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment