1

我在我的应用程序中使用 react-native-navigation 选项卡导航器,在我的一个屏幕上,我有一个可以播放音乐的按钮。但是,如果您按下按钮然后导航到不同的屏幕(使用标签栏),音乐将继续播放。当用户导航到另一个屏幕时,我试图找到一种让音乐停止的方法。

当屏幕发生变化时,我可以调用类似于 componentDidUnmount() 方法的东西吗?

4

2 回答 2

4

v1.0.0-beta.28开始,您现在可以在屏幕中订阅一些选项卡事件(willFocus、didFocus、willBlur、didBlur):

componentDidMount() {
this._sub = this.props.navigation.addListener(
    'willFocus',
    this.yourCallback
  );
}

componentWillUnmount() {
  this._sub.remove();
}

更多细节在这里:https ://github.com/react-navigation/react-navigation/pull/3345

于 2018-02-12T10:53:27.267 回答
1

这里有两个选项(我知道),都不是很好。

First, you can use Redux to to manage your navigator's state and, when tabs change, listen to that change and when the relevant tab is no longer active stop playing the music.

其次,你可以尝试使用onNavigationStateChange,但这对我来说真的很老套——而且我不确定它的实际效果如何。

于 2017-05-23T00:54:17.257 回答