简体   繁体   English

TypeError:无法读取 null react.js 应用程序的属性“classList”

[英]TypeError: Cannot read property 'classList' of null react.js application

I've been struggling with this bug.我一直在努力解决这个错误。 I keep getting TypeError: Cannot read property 'classList' of null on my react application.我不断收到 TypeError: Cannot read property 'classList' of null 在我的反应应用程序上。 I'm quite new to react so have been trying t figure out the issue with no avail.我对反应很陌生,所以一直试图找出问题,但无济于事。 Here's the error and my component, anyone know what the problem is?这是错误和我的组件,有人知道问题是什么吗?

The TypeError Message as text: TypeError 消息作为文本:

←→1 of 8 errors on the page
TypeError: Cannot read property 'classList' of null
(anonymous function)
http://localhost:3003/static/js/main.chunk.js:8044:49
  8041 | var value = window.scrollY;
  8042 | 
  8043 | if (value > 100) {
> 8044 |   document.querySelector('.header--fixed').classList.add('sticky');
       |                                           ^  8045 | } else {
  8046 |   document.querySelector('.header--fixed').classList.remove('sticky');
  8047 | }
View source
This screen is visible only in development. It will not appear if the app crashes in production.
Open your browser’s developer console to further inspect this error.

My Component/ code:我的组件/代码:

import React, { Component } from "react";
import {FaTwitter ,FaInstagram ,FaFacebookF , FaLinkedinIn } from "react-icons/fa";
import { FiX , FiMenu} from "react-icons/fi";
import Scrollspy from 'react-scrollspy'
import mypdf from "../../../public/assets/files/Sarah-Chosen-CV.pdf";

const SocialShare = [
    // {Social: <FaFacebookF /> , link: 'https://www.facebook.com/'},
    // {Social: <FaInstagram /> , link: 'https://www.instagram.com/'},
    // {Social: <FaTwitter /> , link: 'https://twitter.com/'},
    {Social: <FaLinkedinIn /> , link: 'https://www.linkedin.com/in/sarah-chosen/'},
]
class HeaderThree extends Component{
    constructor(props) {
        super(props);
        this.menuTrigger = this.menuTrigger.bind(this);
        this.CLoseMenuTrigger = this.CLoseMenuTrigger.bind(this);
        this.stickyHeader = this.stickyHeader.bind(this);

       //  this.subMetuTrigger = this.subMetuTrigger.bind(this);
        window.addEventListener('load', function() {
            console.log('All assets are loaded');
        })
        
    }
    menuTrigger() {
        document.querySelector('.header-wrapper').classList.toggle('menu-open')
    }
    
    CLoseMenuTrigger() {
        document.querySelector('.header-wrapper').classList.remove('menu-open')
    }

    stickyHeader () {}

    render(){

        window.addEventListener('scroll', function() {
            var value = window.scrollY;
            if (value > 100) {
                document.querySelector('.header--fixed').classList.add('sticky')
            }else{
                document.querySelector('.header--fixed').classList.remove('sticky')
            }
        });

        var elements = document.querySelectorAll('.has-droupdown > a');
        for(var i in elements) {
            if(elements.hasOwnProperty(i)) {
                elements[i].onclick = function() {
                    this.parentElement.querySelector('.submenu').classList.toggle("active");
                    this.classList.toggle("open");
                }
            }
        }
        const { logo, color='default-color' } = this.props;
        let logoUrl;
        if(logo === 'light'){
            logoUrl = <img src="/assets/images/logo/logo-light.png" alt="Digital Agency" />;
        }else if(logo === 'dark'){
            logoUrl = <img src="/assets/images/logo/logo-dark.png" alt="Digital Agency" />;
        }else if(logo === 'symbol-dark'){
            logoUrl = <img src="/assets/images/logo/logo-symbol-dark.png" alt="Digital Agency" />;
        }else if(logo === 'symbol-light'){
            logoUrl = <img src="/assets/images/logo/logo-symbol-light.png" alt="Digital Agency" />;
        }else{
            logoUrl = <img src="/assets/images/logo/logo.png" alt="Digital Agency" />;
        }
        
        return (
          <header
            className={`header-area header-style-two header--fixed ${color}`}
          >
            <div className="header-wrapper">
              <div className="header-left d-flex align-items-center">
                <div className="logo">
                  <a href={this.props.homeLink}>{logoUrl}</a>
                </div>
                <nav className="mainmenunav d-lg-block ml--50">
                  <Scrollspy
                    className="mainmenu"
                    items={[
                      "home",
                      "about",
                      "service",
                      "portfolio",
                      "blog",
                      "contact",
                    ]}
                    currentClassName="is-current"
                    offset={-200}
                  >
                    <li>
                      <a href="#home">Home</a>
                    </li>
                    <li>
                      <a href="#about">About</a>
                    </li>
                    <li>
                      <a href="#service">Skillset</a>
                    </li>
                    <li>
                      <a href="#portfolio">Portfolio</a>
                    </li>
                    <li>
                      <a href="#contact">Contact</a>
                    </li>
                  </Scrollspy>
                </nav>
              </div>
              <div className="header-right">
                <div className="social-share-inner">
                  <ul className="social-share social-style--2 color-black d-flex justify-content-start liststyle">
                    {SocialShare.map((val, i) => (
                      <li key={i}>
                        <a href={`${val.link}`}>{val.Social}</a>
                      </li>
                    ))}
                  </ul>
                </div>
                <div className="header-btn">
                  <a className="rn-btn" href={mypdf} download="Sarah-Chosen-CV.pdf">
                    <span>download CV</span>
                  </a>
                </div>
                {/* Start Humberger Menu  */}
                <div className="humberger-menu d-block d-lg-none pl--20">
                  <span
                    onClick={this.menuTrigger}
                    className="menutrigger text-white"
                  >
                    <FiMenu />
                  </span>
                </div>
                {/* End Humberger Menu  */}
                <div className="close-menu d-block d-lg-none">
                  <span
                    onClick={this.CLoseMenuTrigger}
                    className="closeTrigger"
                  >
                    <FiX />
                  </span>
                </div>
              </div>
            </div>
          </header>
        );
    }
}


export default HeaderThree;

Adding event listeners in react components should happen when the component has its DOM representation is ready.当组件准备好其 DOM 表示时,应该在 React 组件中添加事件侦听器。 componentDidMount is called after the component is mounted and has a DOM representation. componentDidMount在组件被挂载并具有 DOM 表示后被调用。 This is often a place where you would attach generic DOM events.这通常是您附加通用 DOM 事件的地方。

So try to move the code where you assign the listener for scroll event to componentDidMount lifecycle method.因此,请尝试将您为scroll事件分配侦听器的代码移动到componentDidMount生命周期方法。 and also remove the event listener in componentWillUnmount .并删除componentWillUnmount中的事件侦听器。 What makes this more important is that React components will often be re-rendered, and the code to set up event listeners will have the opportunity to run many times.更重要的是,React 组件经常会被重新渲染,而设置事件监听器的代码将有机会运行多次。 This can create errors by setting up multiple listeners where we're actually expecting a single listener run per event occurrence.这可能会通过设置多个侦听器来创建错误,而我们实际上希望每个事件发生时运行一个侦听器。

Additionally, React-based UIs are often used in single-page apps that exist within long-lived browser sessions.此外,基于 React 的 UI 通常用于存在于长期浏览器会话中的单页应用程序中。 This creates an environment where memory leaks can become serious more often.这创造了一个环境,其中 memory 泄漏可能更频繁地变得严重。

Check out this article from react: https://react-cn.github.io/react/tips/dom-event-listeners.html从反应中查看这篇文章: https://react-cn.github.io/react/tips/dom-event-listeners.html

and also this answer describe how to wrap a component with scrollWrapper that handles the scroll event这个答案还描述了如何使用处理滚动事件的 scrollWrapper 包装组件

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

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