2016-06-16 44 views
0

我目前正在處理使用React Native的一些問題。這是場景:我有頁面A,其中包含可點擊頁面的列表,當我單擊一個頁面時,假設頁面B,我可以在頁面A中輸入setState({page: 'B'}),並將當前導航器推送到頁面B,並且當我將導航器重新彈回到頁面A,我想setState({page: ''})在頁面A.但是,我目前沒有辦法檢測到路由已經彈出到頁面A回來,因此我不知道在哪裏把setState代碼。有沒有我失蹤的方法?如何檢測導航彈出回調

回答

3

您可以將一個函數傳遞到將在componentWillUnmount中調用的路由組件。

// in your list component, this really depends on how you set up your 
// Navigator to handle props, hopefully this will give you an idea of how it could work 

this.props.navigator.push(someRoute, {onUnmount:() => this.setState({page: ''})} 

// then, in the routed component 

componentWillUnmount() { 
    this.props.onUnmount() 
}