ReactJs - Creating an “If” component… a good idea?

后端 未结 13 2350
南旧
南旧 2020-12-12 17:11

I\'ve read in the React docs that \"if\" type statements can\'t be used in JSX code, because of the way JSX renders into javascript, it doesn\'t work out as one would expect

13条回答
  •  鱼传尺愫
    2020-12-12 17:23

    The experimental ES7 do syntax is really nice for this situation. If you're using Babel, enable the es7.doExpressions feature then:

    render() {
      var condition = false;
    
      return (
        
    {do { if (condition) { Truthy!; } else { Not truthy.; } }}
    ); }

    See http://wiki.ecmascript.org/doku.php?id=strawman:do_expressions

提交回复
热议问题