简体   繁体   English

localStorge 三元表达式不起作用(反应)

[英]localStorge ternary expression not working (React)

I'm trying to do this.我正在尝试这样做。

const isUserConnected = localStorage.getItem('token')

{isUserConnected.length ? null : 
      <div className='registerLogin'>
     Register...
      </div>
}

The ternary isn't acting in accordance to the localStorage content.三元不按照 localStorage 内容行事。

What is wrong?怎么了?

isUserConnected is probably undefined . isUserConnected可能是undefined

In which case your code should look more like在这种情况下,您的代码应该看起来更像

const token = localStorage.getItem('token')
const isUserConnected = token && token.length

{isUserConnected &&
  <div className='registerLogin'>
    Register...
  </div>
}

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

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