Advertisement
karlakmkj

React (JSX) - if condition in render()

Jan 25th, 2021
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import ReactDOM from 'react-dom';
  3.  
  4. const fiftyFifty = Math.random() < 0.5;
  5.  
  6. // New component class starts here:
  7. class TonightsPlan extends React.Component{
  8.   render(){
  9.     if (fiftyFifty) {
  10.      return <h1>Tonight I'm going out WOOO</h1>; //place return inside each condition
  11.    }
  12.    else {
  13.      return <h1>Tonight I'm going to bed WOOO</h1>;
  14.     }
  15.   }
  16. }
  17. ReactDOM.render(<TonightsPlan />, document.getElementById('app'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement