2016-06-15 1 views
2

jQuery 위젯과 같은 메소드를 초기화 한 경우 initialize가 한 번만 호출되지만 둘 다 호출된다는 것을 어떻게 확인할 수 있습니까? 새로 고침없이 페이지를 탐색 할 때 사용합니다. 내 현재 접근 방식은 아래와 같습니다 :componentDidUpdate() 및 componentDidMount()에서 한 번만 실행되는지 확인하십시오.

// key used to make sure initialize() only runs in one of DidUpdate/DidMount 
let key = 0; 

Component = React.createClass({ 
    mixins: [ReactMeteorData], 
    hasKey() { 
    return key; 
    }, 
    getKey() { 
    key = 1; 
    }, 
    resetKey() { 
    key = 0; 
    }, 
    componentDidUpdate() { 
    if (!(this.hasKey())) { 
     initialize()(); 
     this.getKey(); 
    } 
    }, 
    componentDidMount() { 
    if (!(this.hasKey())) { 
     initialize()(); 
     this.getKey(); 
    } 
    }, 
    componentWillUnmount() { 
    this.resetKey(); 
    } 
}}) 
+1

정확하게 'componentDidMount'에 이니셜 라이저를 포함하여 질문을 올바르게 이해하고 있다면 필요한 것이어야합니다. 이 함수는 구성 요소가 DOM에 마운트 될 때 한 번만 실행됩니다. 페이지를 새로 고치거나 페이지를 탐색하면이 구성 요소가 탑재되어 초기화 도구가 트리거됩니다. –

답변

0

기본적으로 생성자에 속성을 만들 수 있습니다 (예 : false). didMount, didUpdate. 그리고 componentDidMount는 한 번만 호출되므로 this.didMount = true을 넣을 수 있습니다. 업데이트 후 매번 componentDidUpdate가 호출되므로 다음을 확인하십시오. !this.didUpdate = (this.didUpdate = true); 그런 다음이 속성 만 선택하십시오.