简体   繁体   English

TSX 解析错误:箭头 function 上预期的表达式

[英]TSX Parsing error:Expression expected on arrow function

How can i solve this error?我该如何解决这个错误? TS1109: Expression expected. I'm new to typescript and I don't understand why is error popping up.我是 typescript 的新手,我不明白为什么会弹出错误。 Any suggestions please?请问有什么建议吗?

  const checkIfUserIsLoggedIn = () : boolean => {
    return if (localStorage.getItem("logged-in") === "true" ) {
      return <LoggedInMp/>
    } else {
      return <NotLoggedInMp/>
    }
  }

You have a few errors in your code.您的代码中有一些错误。 The return in front of the if is wrong and the return type of your function may also be off. if前面的return是错误的,你的function的return type也可能是off。 The first return is causing the error, because an expression is expected and a if {} else {} is not an expression.第一次return导致错误,因为需要一个表达式,而if {} else {}不是表达式。

const checkIfUserIsLoggedIn = () => {
    if (localStorage.getItem("logged-in") === "true" ) {
      return <LoggedInMp/>;
    } else {
      return <NotLoggedInMp/>;
    }
}

Additional to the accepted answer, you could even make this an one-liner:除了接受的答案之外,您甚至可以将其设为单行:

const checkIfUserIsLoggedIn = () => localStorage.getItem("logged-in") ? <LoggedInMp/> : <NotLoggedInMp/>;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM