diff --git a/packages/taro-router-rn/src/TaroProvider.js b/packages/taro-router-rn/src/TaroProvider.js index 16827c8648f2..46847f0555ad 100644 --- a/packages/taro-router-rn/src/TaroProvider.js +++ b/packages/taro-router-rn/src/TaroProvider.js @@ -10,22 +10,31 @@ class TaroProvider extends React.Component { this.refreshProviderRef = React.createRef() } - componentDidMount () { + navigationMethodInit () { let {Taro} = this.props + Taro.navigateTo = this.wxNavigateTo.bind(this) + Taro.redirectTo = this.wxRedirectTo.bind(this) + Taro.navigateBack = this.wxNavigateBack.bind(this) + Taro.switchTab = this.wxSwitchTab.bind(this) + Taro.getCurrentPages = this.wxGetCurrentPages.bind(this) + Taro.showTabBar = this.showTabBar.bind(this) + Taro.hideTabBar = this.hideTabBar.bind(this) + } + + componentWillMount () { + this.navigationMethodInit() // didFocus this.didFocusSubscription = this.props.navigation.addListener( 'didFocus', payload => { // 页面进入后回退并不会调用 React 生命周期,需要在路由生命周期中绑定 this - Taro.navigateTo = this.wxNavigateTo.bind(this) - Taro.redirectTo = this.wxRedirectTo.bind(this) - Taro.navigateBack = this.wxNavigateBack.bind(this) - Taro.switchTab = this.wxSwitchTab.bind(this) - Taro.getCurrentPages = this.wxGetCurrentPages.bind(this) - Taro.showTabBar = this.showTabBar.bind(this) - Taro.hideTabBar = this.hideTabBar.bind(this) + this.navigationMethodInit() } ) + } + + componentDidMount () { + let {Taro} = this.props try { Taro.startPullDownRefresh = this.refreshProviderRef.current && this.refreshProviderRef.current.handlePullDownRefresh Taro.stopPullDownRefresh = this.refreshProviderRef.current && this.refreshProviderRef.current.stopPullDownRefresh diff --git a/packages/taro-router-rn/src/getWrappedScreen.js b/packages/taro-router-rn/src/getWrappedScreen.js index 390e0c7bede8..c2e54dc6de94 100644 --- a/packages/taro-router-rn/src/getWrappedScreen.js +++ b/packages/taro-router-rn/src/getWrappedScreen.js @@ -116,16 +116,21 @@ function getWrappedScreen (Screen, Taro, globalNavigationOptions = {}) { } } - componentDidMount () { + navigationMethodInit () { + Taro.setNavigationBarTitle = this.setNavigationBarTitle.bind(this) + Taro.setNavigationBarColor = this.setNavigationBarColor.bind(this) + Taro.showNavigationBarLoading = this.showNavigationBarLoading.bind(this) + Taro.hideNavigationBarLoading = this.hideNavigationBarLoading.bind(this) + } + + componentWillMount () { + this.navigationMethodInit() // didFocus this.didFocusSubscription = this.props.navigation.addListener( 'didFocus', payload => { // 页面进入后回退并不会调用 React 生命周期,需要在路由生命周期中绑定 this - Taro.setNavigationBarTitle = this.setNavigationBarTitle.bind(this) - Taro.setNavigationBarColor = this.setNavigationBarColor.bind(this) - Taro.showNavigationBarLoading = this.showNavigationBarLoading.bind(this) - Taro.hideNavigationBarLoading = this.hideNavigationBarLoading.bind(this) + this.navigationMethodInit() // 页面聚焦时,调用 componentDidShow this.getScreenInstance().componentDidShow && this.getScreenInstance().componentDidShow() }