简体   繁体   English

无法在尚未使用事件侦听器挂载的组件上调用 setState

[英]Can't call setState on a component that is not yet mounted using Event Listeners

I'm getting this warning:我收到这个警告:

Warning: Can't call setState on a component that is not yet mounted.警告:无法在尚未安装的组件上调用 setState。 This is a no-op, but it might indicate a bug in your application.这是一个无操作,但它可能表明您的应用程序中存在错误。 Instead, assign to this.state directly or define a state = {};相反,直接分配给this.state或定义一个state = {}; class property with the desired state in the Navbar component. class 属性在导航栏组件中具有所需的 state。

I created a Navbar component with a NavMin component that only renders when the screen has a width of 11000 or less.我创建了一个带有 NavMin 组件的 Navbar 组件,该组件仅在屏幕宽度为 11000 或更小时才呈现。 I think it has something to do with using componentDidMount and componentWillUnmount but I am not sure how to properly use it.我认为这与使用 componentDidMount 和 componentWillUnmount 有关,但我不确定如何正确使用它。 I have tried using componentWillUnmount in NavMin but I receive this error:我曾尝试在 NavMin 中使用 componentWillUnmount,但收到此错误:

Can't perform a React state update on an unmounted component.无法对未安装的组件执行 React state 更新。 This is a no-op, but it indicates a memory leak in your application.这是一个无操作,但它表明您的应用程序中存在 memory 泄漏。 To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.要修复,请取消 componentWillUnmount 方法中的所有订阅和异步任务。

Here are the two components:这是两个组件:

import  React, { Component } from 'react';
import { Link } from 'react-router-dom';
import bars from './Images/bars.png';
import './NavMini.css'

class NavMini  extends Component {

    componentWillUnmount(){

    }
    constructor(props){
    super(props)
        this.state = {
            showMenu: false 
        }

        this.showMenu = this.showMenu.bind(this)
        this.closeMenu = this.closeMenu.bind(this)
    }

  showMenu(event){
       event.preventDefault();

       this.setState({
           showMenu: true
       },
       () => {
           document.addEventListener('click', this.closeMenu);
       });
   }

   closeMenu(){
       this.setState({
           showMenu: false 
       }, () => {
           document.removeEventListener('click', this.closeMenu);
       })
   }

    render(){
        return(
            <div id="Navigation-mini"> 
            <button  onClick={this.showMenu} className="button-nav">
                 <img className="Mini-nav" src={bars} alt="nav-icon"/>
            </button>
            {
            this.state.showMenu
                ? (
                <div className="Navmenu">
                    <Link className="NavLink-mini" to='/'>Home</Link>
                    <Link className="NavLink-mini" to='/About'>About</Link>
                    <Link className="NavLink-mini" to='/Blog'>Blog</Link>
                    <Link className="NavLink-mini" to='/Contact'>Contact</Link>
                </div>
                )
                : (
                    null
                )
            }

         </div>
        )
    }
}

export default NavMini
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
import Search from './Images/Search.png';
import Logo from './Images/Icon.png';
import NavMini from './NavMini'
import './NavBar.css';


class Navbar extends Component{
    constructor(props){
        super(props)

        let width = window.innerWidth;

        this.state = {
            show: false 
        }

        if(width < 1100){
            this.setState({
                show: true
            })
        }
    }

    render(){
    return (
        <div>
        <div id="Navigation">
            <div id="NavLogo">
                <Link to='/'><img id="CodeDestinee" src={Logo} alt="Logo" /></Link>
                    <h3 id="LogoName">Code <br/> Destinee</h3>
            </div>

            <div id="Navbar">
                <Link className="NavLink" to='/'>Home</Link>
                <Link className="NavLink" to='/About'>About</Link>
                <Link className="NavLink" to='/Blog'>Blog</Link>
                <Link className="NavLink" to='/Contact'>Contact</Link>
                <img className="NavLink" id="SearchIcon" src={Search} alt="Search-Icon" />
            </div>
        </div>

        { !this.state.show ? <NavMini /> : null } 
        </div>
    )
    }
}

export default Navbar

Can someone help me understand what I am doing wrong?有人可以帮我理解我做错了什么吗?

You are using this.setState in constructor, please change it to您在构造函数中使用 this.setState,请将其更改为

if(width < 1100){
    this.state  = {
        show: true
    }
}

As pointed out above, you're calling setState in the constructor which you shouldn't be doing.如上所述,您在构造函数中调用 setState 是不应该做的。 The above solution may work, but I believe a better solution is to set show to true or false in one expression, depending on the width:上述解决方案可能有效,但我认为更好的解决方案是在一个表达式中将show设置为 true 或 false,具体取决于宽度:

this.state = {
    show: width < 1100 
}

This will set this.state.show to true or false, depending on the width.这会将this.state.show设置为 true 或 false,具体取决于宽度。

componentDidMount() {
  let width = window.innerWidth;
  if(width < 1100){
      this.setState({
          show: true
      })
  }
}

Constructor is the only place where you should assign this.state directly.构造函数是唯一应该直接分配this.state的地方。 In all other methods, you need to use this.setState() instead.在所有其他方法中,您需要改用this.setState()

暂无
暂无

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

相关问题 无法在尚未安装的组件上调用 setState - Can't call setState on a component that is not yet mounted 无法在尚未安装的组件上调用setState-TSX - Can't call setState on a Component that is not yet mounted - tsx 反应应用程序。 无法在尚未安装的组件上调用 setState - React app. Can't call setState on a component that is not yet mounted Reactjs - 无法在尚未安装的组件上调用 setState - Reactjs - Can't call setState on a component that is not yet mounted 无法在尚未安装的组件上调用 setState - 反应原生 - Can't call setState on a component that is not yet mounted - react native 警告:无法在尚未安装的组件上调用 setState - Warning: Can't call setState on a component that is not yet mounted React JS - 无法在尚未安装的组件上调用 setState - React JS - can't call setState on a component that is not yet mounted 如何使用React + Firebase编辑数据库中的文档信息? 无法在尚未安装的组件上调用setState - How to edit information of document in databse using React + Firebase? Can't call setState on a component that is not yet mounted 警告:无法在尚未安装的组件上调用 setState。 这是一个无操作,但它可能表明您的应用程序中存在错误 - Warning: Can’t call setState on a component that is not yet mounted. This is a no-op, but it might indicate a bug in your application 我试图设置我的 state,但它返回“无法在尚未安装的组件上调用 setState”。 错误 - I tried to set my state, but it returns “Can't call setState on a component that is not yet mounted.” error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM