【问题标题】:Lifecycle Management in React-nativeReact-native 中的生命周期管理
【发布时间】:2019-10-22 10:15:56
【问题描述】:

在 iOS 模拟器中使用 Swipe 退出应用时的生命周期是什么?我试图记录每个生命周期,但什么也没发生。我想在应用程序退出时执行特定功能,但我不确定该怎么做。有没有人和我有类似的问题?

【问题讨论】:

  • 退出是指真正杀死应用程序吗?把它刷掉?
  • @Auticcat 杀死应用程序
  • 在 iOS 中使用 applicationWillTerminate(_ application: UIApplication) 函数。从那里你可以调用你共享的 React-Native 组件函数

标签: javascript react-native lifecycle react-lifecycle


【解决方案1】:

无法检测应用是否正在关闭,但您可以使用AppState 检查应用是否在后台。

在你的父组件中,添加:

import { AppState } from 'react-native';

...

componentDidMount() {
    AppState.addEventListener('change', this._handleAppStateChange);
}

...

_handleAppStateChange = (nextAppState) => {
    //based of nextAppState, do what you need
}

来源:https://facebook.github.io/react-native/docs/appstate

【讨论】:

    【解决方案2】:

    在 iOS AppDelegate.swift 中

    func applicationWillTerminate(_ application: UIApplication) {
        // get your shared RCTView
        sharedRCTView.appProperties = [..., "isTerminate": true];
    }
    

    在 React-Native 组件中

    static getDerivedStateFromProps(nextProps, prevState){
       if(nextProps.isTerminate!==prevState.isTerminate){
         // Do your stuff
         return { someState: nextProps.isTerminate};
      }
      else return null;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-12
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-07
      相关资源
      最近更新 更多