JS-React: if-else Condition In The render()

HTML(none)
<div id="app"></div>

CSS(sass)
html, body
  height: 100%
  
body
  background: #333
  display: flex
  justify-content: center
  align-items: center
  font-family: Helvetica Neue
  
h1
  font-size: 2em
  color: #eee
  display: inline-block


JAVASCRIPT(babel)
class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      display: true
    }
    this.leopardDisplay = this.leopardDisplay.bind(this);
  }
  leopardDisplay() {
    this.setState({
      display: !this.state.display
    });
  }
  render() {
    if(this.state.display) return (
       <div>
         <button onClick={this.leopardDisplay}>Snow Leopard Hide</button>
         <h1><img src="http://st-gdefon.gallery.world/wallpapers_original/346026_gallery.world.jpg" width="700" height="300"/></h1>
       </div>
    );
    
    else return (  <div>
         <button onClick={this.leopardDisplay}>Snow Leopard Show</button>
       </div>);
  }
};
React.render(<MyComponent />, document.getElementById('app'));

The other way to render();
render() {
    return (
      <div>
        <button onClick={this.leapardDisplay}>Snow leopard Display</button>
        {this.state.display && <h1><img src="http://st-gdefon.gallery.world/wallpapers_original/346026_gallery.world.jpg" width="700" height="300"/></h1>}
      </div>
    );

  }

Comments

Popular posts from this blog

Tech Duos For Web Development

CIFAR-10 Dataset Classification Using Convolutional Neural Networks (CNNs) With PyTorch

Long-short-term-memory (LSTM) Word Prediction With PyTorch