ternary operator in jsx to include html with react

前端 未结 6 1872
离开以前
离开以前 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:04

    The accepted answer by @Nathan and other similar answers are correct. But it's worth noting that the result for ? and the result for : must each be a single element or wrapped in a single element (or the result may be null | undefined, either of which qualifies as a single element). In the example below, the result for ? will work but the result for : will fail....

    return (
      {this.state.message === 'failed' ? (
          
    three elements wrapped inside another element work.
    ) : ( html like haiku must follow rules of structure. ) } )

提交回复
热议问题