Skip to content

Commit

Permalink
fix(RN): 修复页面返回不掉用 componentDidShow 和 componentDidHide 的 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Pines-Cheng committed Dec 29, 2018
1 parent 8f1d865 commit 90901cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/taro-router-rn/src/TaroProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class TaroProvider extends React.Component {

componentDidMount () {
let {Taro} = this.props
this.didBlurSubscription = this.props.navigation.addListener(
// didFocus
this.didFocusSubscription = this.props.navigation.addListener(
'didFocus',
payload => {
// 页面进入后回退并不会调用 React 生命周期,需要在路由生命周期中绑定 this
Expand Down Expand Up @@ -123,7 +124,7 @@ class TaroProvider extends React.Component {

componentWillUnmount () {
// Remove the listener when you are done
this.didBlurSubscription && this.didBlurSubscription.remove()
this.didFocusSubscription && this.didFocusSubscription.remove()
}

render () {
Expand Down
19 changes: 15 additions & 4 deletions packages/taro-router-rn/src/getWrappedScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,35 @@ function getWrappedScreen (Screen, Taro, globalNavigationOptions = {}) {
}

componentDidMount () {
this.didBlurSubscription = this.props.navigation.addListener(
// 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)
// 页面聚焦时,调用 componentDidShow
this.getScreenInstance().componentDidShow && this.getScreenInstance().componentDidShow()
}
)

// willBlur
this.willBlurSubscription = this.props.navigation.addListener(
'willBlur',
payload => {
// 页面将失去焦点,调用 componentDidHide
this.getScreenInstance().componentDidHide && this.getScreenInstance().componentDidHide()
}
)
this.getScreenInstance().componentDidShow && this.getScreenInstance().componentDidShow()
this.screenRef.current && this.setState({}) // TODO 不然 current 为null ??
}

componentWillUnmount () {
this.getScreenInstance().componentDidHide && this.getScreenInstance().componentDidHide()
// Remove the listener when you are done
this.didBlurSubscription && this.didBlurSubscription.remove()
this.didFocusSubscription && this.didFocusSubscription.remove()
this.willBlurSubscription && this.willBlurSubscription.remove()
}

render () {
Expand Down

0 comments on commit 90901cc

Please sign in to comment.