出现屏幕时,将调用哪个生命周期事件



假设我有两个屏幕:

  • 屏幕
  • 屏幕B

我最初被降落在屏幕上a 。当我单击Button时,我会导航到屏幕B 。当我按Back Button时,我再次被导航到屏幕a

我想在上述方案中提到的屏幕时致电动作创建者。

我只想知道每次显示屏幕时都会调用哪个生命周期事件。

是否有某些事件,例如componentWillAppear()

注意:我正在将react-nativereact-navigation一起进行导航。

现在可以通过他们的听众进行简单的反应导航:

在您的组件中

componentDidMount = () => {
  this._componentFocused();
  this._sub = this.props.navigation.addListener(
    'didFocus',
    this._componentFocused
  );
}
componentWillUnmount() {
  this._sub.remove();
}
_componentFocused = () => {
  this.setState({dataToShow});
}

React导航文档 - 生命周期

从一个屏幕导航到另一个屏幕时,第一个屏幕不会被卸载,但仍在堆栈中,只需隐藏在后台。

您可能需要的是componentDidFocus,但目前尚不可用,请参见React-Native Open Issue#51。

您可以尝试这种替代方式:React-Navigation-Addons。有了这个事件,您可以进行focus&blur可能适合您的需求。

如果使用React-Native-Navigation,可以收听外观事件:https://wix.github.io/react-native-navigation/#/screen-api?id?id=可视性 - events-globally

最新更新