All notes
Jsx

Examples

Concatenate vars into string



// https://stackoverflow.com/questions/39523040/concatenating-variables-and-strings-in-react

href={"#demo" + this.state.id}

href={`#demo${this.state.id}`}

How to Add If…Else Statements

devnacho.com.

Assigning the JSX blocks to a variable

This approach is prefered because it keeps the return section pretty clean.



class HelloMessage extends React.Component {
  render (){
    let userMessage;
    if (this.props.loggedIn) {
      userMessage = (
        <span>
          <h2>{ `Welcome Back ${ this.props.name }` }</h2>
          <p>You can visit settings to reset your password</p>
        </span>
      )
    } else {
      userMessage = (
        <h2>Hey man! Sign in to see this section</h2>
      )
    }
    return(
      <div>
        <h1>My Super React App</h1>
        { userMessage }
      </div>
    )
  }
}

Big ternary condition



class HelloMessage extends React.Component {
  render (){
    return(
      <div>
        <h1>My Super React App</h1>
        { this.props.loggedIn ?
            <span>
              <h2>{ `Welcome Back ${ this.props.name }` }</h2>
              <p>You can visit settings to reset your password</p>
            </span>
            :
            <h2>Hey man! Log in to see this section</h2>
        }
      </div>
    )
  }
}