简体   繁体   中英

Next.js localstorage value always return false

I'm trying to check is localStorage value is correct. Localstorage value is successfully saved. I wonder why it always return false.

console.log('isTrue',localStorage.getItem('third') == "2") // false
console.log('value',localStorage.getItem('third'))  // "2"
localStorage.setItem('third', JSON.stringify(third)) // save like this

在此处输入图像描述

在此处输入图像描述

Can someone help what is wrong? Thanks!

Check the API spec for local storage and the get/set methods, particularly the return type of .getItem - you'll need to JSON.parse the store value before doing comparisons between other data or doing other work with it, ie

console.log('isTrue', JSON.parse(localStorage.getItem('third')) === "2") --> true

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