【问题标题】:React Native 'Unexpected token' errorReact Native 'Unexpected token' 错误
【发布时间】:2016-03-03 08:29:35
【问题描述】:

我在 5:7 收到“意外令牌”。不明白为什么。

var Navbar = React.createClass({
  render: function() {
    const { dispatch, isAuthenticated, errorMessage } = this.props
    return (
      {!isAuthenticated &&
         <Login
           errorMessage={errorMessage}
           onLoginClick={ () => dispatch(login()) }
         />
       }
       {isAuthenticated &&
         <Logout onLogoutClick={() => dispatch(logoutUser())} />
       }
    );
  }
});

【问题讨论】:

    标签: javascript syntax-error react-native


    【解决方案1】:

    你需要这样重写它:

    render: function() {
        const { dispatch, isAuthenticated, errorMessage } = this.props
        var rendered = !isAuthenticated ? (
          <Login
            errorMessage={errorMessage}
            onLoginClick={ () => dispatch(login()) }
          />
        ) : (
          <Logout onLogoutClick={() => dispatch(logoutUser())} />
        );
        return rendered;
      }
    

    这是一个三元运算符。您可以在here了解更多信息。

    基本上语法是这样的:condition ? expr1 : expr2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多