ternary operator in jsx to include html with react

前端 未结 6 1857
离开以前
离开以前 2020-12-23 11:12

I\'m using react and I\'m trying to display this error message if this.state.message === \'failed\'. But I\'m really not sure why this ternary operation isn\'t

6条回答
  •  感动是毒
    2020-12-23 12:06

    I currently like to format my ternaries like this in react:

    render () {
      return (
        
    { //Check if message failed (this.state.message === 'failed') ?
    Something went wrong
    :
    Everything in the world is fine
    }
    ); }

    You are correct that IIFEs can be used within a render statement as well as ternary expressions. Using a normal if .. else statement is valid, but the render function's return statement can only contain expressions so you would have to do those elsewhere..

提交回复
热议问题