Skip to content

Instantly share code, notes, and snippets.

@react-ram
Created October 22, 2019 10:04
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 react-ram/5fbf1e677d83d4caaba7e649baeecf15 to your computer and use it in GitHub Desktop.
Save react-ram/5fbf1e677d83d4caaba7e649baeecf15 to your computer and use it in GitHub Desktop.
basics - conditional rendering with inline conditional or ternery operator ?: example
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