Created
October 22, 2019 10:04
-
-
Save react-ram/5fbf1e677d83d4caaba7e649baeecf15 to your computer and use it in GitHub Desktop.
basics - conditional rendering with inline conditional or ternery operator ?: example
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"; | |
export class App extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
isLoggedin: true | |
}; | |
this.loginHandler = this.loginHandler.bind(this); | |
this.logoutHandler = this.logoutHandler.bind(this); | |
} | |
loginHandler() { | |
this.setState({ | |
isLoggedin: true | |
}); | |
} | |
logoutHandler() { | |
this.setState({ | |
isLoggedin: false | |
}); | |
} | |
render() { | |
const { isLoggedin } = this.state; | |
return ( | |
<div> | |
<h2>the user is {isLoggedin ? "currently" : "not"} logged in</h2> | |
{isLoggedin ? ( | |
<LogoutButon onClick={this.logoutHandler} /> | |
) : ( | |
<LoginButton onClick={this.loginHandler} /> | |
)} | |
</div> | |
); | |
} | |
} | |
function LoginButton(props) { | |
return <button onClick={props.onClick}>login</button>; | |
} | |
function LogoutButon(props) { | |
return <button onClick={props.onClick}>logout</button>; | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment