Skip to content

Instantly share code, notes, and snippets.

@hadesunseenn
Created November 22, 2020 00:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hadesunseenn/71d96adf937a8d676d0ce74a296a3c85 to your computer and use it in GitHub Desktop.
Save hadesunseenn/71d96adf937a8d676d0ce74a296a3c85 to your computer and use it in GitHub Desktop.
React use if else condition in render function
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