简体   繁体   中英

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.

What is wrong?

isUserConnected is probably 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>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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