if statement is a fine way to conditionally render a component

Solutions on MaxInterview for if statement is a fine way to conditionally render a component by the best coders in the world

showing results for - "if statement is a fine way to conditionally render a component"
Monica
28 Jul 2016
1class LoginControl extends React.Component {
2  constructor(props) {
3    super(props);
4    this.handleLoginClick = this.handleLoginClick.bind(this);
5    this.handleLogoutClick = this.handleLogoutClick.bind(this);
6    this.state = {isLoggedIn: false};
7  }
8
9  handleLoginClick() {
10    this.setState({isLoggedIn: true});
11  }
12
13  handleLogoutClick() {
14    this.setState({isLoggedIn: false});
15  }
16
17  render() {
18    const isLoggedIn = this.state.isLoggedIn;
19    let button;
20    if (isLoggedIn) {      button = <LogoutButton onClick={this.handleLogoutClick} />;    } else {      button = <LoginButton onClick={this.handleLoginClick} />;    }
21    return (
22      <div>
23        <Greeting isLoggedIn={isLoggedIn} />        {button}      </div>
24    );
25  }
26}
27
28ReactDOM.render(
29  <LoginControl />,
30  document.getElementById('root')
31);