javascript - React/Javascript 比较运算符不工作
问题描述
不知何故,我无法让我的比较运算符工作。我有以下代码:
function handleNotifClickRemind(key) {
localStorage.setItem('no_remind_change_pwd', true);
notification.close(key);
}
// ...
<Button outline size="small" onClick={() => handleNotifClickRemind(key)}>
Dont remind me
</Button>
// ...
console.log(localStorage.getItem('no_remind_change_pwd'));
function another_func(data) {
if (localStorage.getItem('no_remind_change_pwd') != true) {
openNotification('topRight');
}
}
当我单击“不提醒我”按钮时,handleNotifClickRemind
由于第三部分的日志输出打印而被触发true
。然而,openNotification
还是被触发了。有人可以帮忙吗?
PS我没有初始化值,我只是让它no_remind_change_pwd
为空。
提前致谢。
解决方案
您在本地存储中保存的所有内容都是字符串。所以你在比较"true"!=true
哪个总是true
。
如果要比较值,可以使用如下。
JSON.parse(localStorage.getItem('no_remind_change_pwd')) != true
这意味着,以下是true
console.log(JSON.parse("true") === true)
推荐阅读
- html5-canvas - 在 konvajs 中动画阶段滚动
- python - Python scrapy:如何通过检测同一级别的类来抓取链接?
- .net - 无法将当前 JSON 对象反序列化为“System.Collections.Generic.List”类型,因为该类型需要 JSON 数组才能正确反序列化
- python - 我正在使用 Outlook 2016 和 Windows 7 企业版。想自动提取附件
- angular - Angular中的动画饼图
- ios - 当我们长按然后在ios中拖动时,单击效果背景不适用于按钮
- java - Java - 仅比较特定键的两个 Maps 条目
- python - 如何在 Python 中获得连续天数
- hazelcast - Hazlecast 通过数字查找查找范围
- mysql - 如何设计一个将多列合并为一个的数据库?