简体   繁体   English

反应:类型错误:无法读取 null 的属性“getItem”

[英]React: TypeError: Cannot read property 'getItem' of null

I'm created a simple React app and added a banner.我创建了一个简单的 React 应用程序并添加了一个横幅。 But I'm getting a lot this error in Sentry from users.但是我在 Sentry 中从用户那里收到了很多这个错误。

Cannot read properties of null (reading 'getItem');无法读取 null 的属性(读取“getItem”);

If someone knows what is a problem tell me please如果有人知道有什么问题请告诉我

setTimeout(() => {
  const banner = document.getElementById("banner");

  if (!window.localStorage?.getItem("seen")) {
    const button = document.getElementById("hide");

    if(button){
      button.addEventListener('click', () => {
      window.localStorage?.setItem("seen", 'true');
        banner.style.bottom = "-10vh";
        setTimeout(() => banner.remove(), 2000);
      });

      banner.style.removeProperty("visibility");
      setTimeout(() => banner.style.removeProperty("bottom"), 100);
    }
  } else banner?.remove();
}, 2000);

That is probably the key: value getItem is empty or undefined in local storage thus you are getting that error, goto local storage on the browser and check your key-value pair in local storage这可能是关键:值 getItem 在本地存储中为空或未定义因此您会收到该错误,请转到浏览器上的本地存储并检查本地存储中的键值对

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

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