2017-05-23 7 views
1

ウィンドウイベントリスナーにバインドされている関数がアンマウントされました。それでも、次のページに行くと、イベント内の関数はまだ削除されていますが実行されますか?ここで何が問題だろうか?ここで関数はアンマウントされていますが、まだイベントリスナーで実行中

componentDidMount(){ 
    window.addEventListener("resize", this.updateDimensions.bind(this)); 
    } 
    componentWillUnmount(){ 
    console.log("unmounting...."); 
    window.removeEventListener('resize', this.updateDimensions.bind(this)); 
    } 

がイベントにバインドさ-装着されている機能です。

updateDimensions(){ 
     if (this.refs.get_it.clientWidth < 774){ 
     this.setState({ 
     width:this.refs.get_it.clientWidth, 
     height:400, 
     flag:true}); 
     } 
    } 

答えて

4

若干の混乱があなたのコードであり

componentDidMount(){ 
     window.addEventListener("resize", this.updateDimensions.bind(this)); 
     // first instance listening to event 
    } 
    componentWillUnmount(){ 
     console.log("unmounting...."); 
     window.removeEventListener('resize', this.updateDimensions.bind(this)); 
     // second instance removed from listener here first!== second 
    } 

ちょうどこの

constructor(props) { 
     super(props); 
     this.updateDimensions = this.updateDimensions.bind(this); 
    } 
    componentDidMount(){ 
     window.addEventListener("resize", this.updateDimensions); 
     // first instance listening to event 
    } 
    componentWillUnmount(){ 
     console.log("unmounting...."); 
     window.removeEventListener('resize', this.updateDimensions); 
     // same instance removed from listener here first == second 
    } 
+2

をお試しください同じものを書こうとしました-_- –

+0

Tnx aたくさん、私の時間を節約し、常にコンストラクタで今から、悪い練習を私のメソッドをバインドします。 –

+0

あなたを助ける楽しみ。答えを受け入れることもできますか? – duwalanise

関連する問題