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

后端 未结 13 2357
南旧
南旧 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:20

    You can do a self-executing function, with the right context too (with the help of Babel though)! Which I prefer because there's no need for assigning of variables and you can be as complex as you want (though you probably shouldn't for the sake of maintainability):

    render() {
      return (
        
    Hello!
    {() => { if (this.props.isLoggedIn) { return } else { return } }()}
    Another Tag
    ); }

提交回复
热议问题