React警告: Warning: Can‘t perform a React state update on an unmounted component.

React警告: Warning: Can’t perform a React state update on an unmounted component.

This is a warning. The reason for the error is that state cannot be set after the component is destroyed.
Solution:
1. Clear all timers

componentDidMount(){
    
    
              let timer= setInterval(()=>{
    
    
                    let {
    
    newsArr}=this.state
                    let content='新闻'+(newsArr.length+1)
                    this.setState({
    
    
                        newsArr:[content,...newsArr]
                    })
                },1000)
            }
            close=()=>{
    
    
               ReactDOM.unmountComponentAtNode(document.getElementById('test'))
            }
              componentWillUnmount(){
    
    
                console.log('将要卸载')
                clearInterval(this.timer)
            }

2. When the setState operation is performed after the asynchronous execution is completed, the component information cannot be obtained, causing this exception.

  componentWillUnmount(){
    
    
                console.log('将要卸载')
                clearInterval(this.timer)
                this.setState = ()=>false
            }

Guess you like

Origin blog.csdn.net/Stars_in_rain/article/details/122979248