RN使用
createBottomTabNavigator
創建底部導航欄時,只會在第一次創建時調用 componentDidMount 鈎子,當跳轉到另一個頁面,點擊返回鍵再次返回到此頁面時不會再次調用,但我們往往需要在返回此頁面后重新執行獲取數據等方法函數。
解決方法
componentDidMount() {
this._navListener = this.props.navigation.addListener('didFocus', () => {
this.getData();
});
}
componentWillUnmount() {
this._navListener.remove();
}
監聽路由,即使頁面沒有被重新渲染,當路由 didFocus
時,也會執行回調函數。