【问题标题】:How to detect OnResume and onPause on react native Es6如何在反应原生 Es6 上检测 OnResume 和 onPause
【发布时间】:2018-10-05 19:35:52
【问题描述】:

如何检测从后台到前台 es6 的任何方法?

这在 React-native 中可行吗?是否有任何类或工具可以帮助这种类型的方法?

【问题讨论】:

    标签: android react-native-android onresume onpause


    【解决方案1】:

    试试这个,它对我有用

    import React, {Component} from 'react'
    import {AppState, Text} from 'react-native'
    
    class AppStateExample extends Component {
    
      state = {
        appState: AppState.currentState
      }
    
      componentDidMount() {
        AppState.addEventListener('change', this._handleAppStateChange);
      }
    
      componentWillUnmount() {
        AppState.removeEventListener('change', this._handleAppStateChange);
      }
    
      _handleAppStateChange = (nextAppState) => {
        if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
          console.log('App has come to the foreground!')
        }
        this.setState({appState: nextAppState});
      }
    
      render() {
        return (
          <Text>Current state is: {this.state.appState}</Text>
        );
      }
    
    }
    

    【讨论】:

    • 组件状态呢?就像如果我们加载一个场景并在它上面加载另一个场景!是否有任何触发方法,如 onPause OnResume 特定于组件而不是特定于 App?
    【解决方案2】:

    作为对现有答案的更新,removeEventListener(type, handler) 已成为 deprecated(自 2021 年 9 月起):

    对返回的事件订阅使用 remove() 方法 addEventListener()

    使用类组件

    import React, {Component} from 'react'
    import {AppState, Text} from 'react-native'
    
    class AppStateExample extends Component {
    
      state = {
        appState: AppState.currentState
      }
    
      eventListenerSubscription = null;
    
      componentDidMount() {
        this.eventListenerSubscription = AppState.addEventListener('change', this._handleAppStateChange);
      }
    
      componentWillUnmount() {
        this.eventListenerSubscription.remove();
      }
    
      _handleAppStateChange = (nextAppState) => {
        if (this.state.appState.match(/inactive|background/) && nextAppState === 'active') {
          console.log('App has come to the foreground!')
        }
        this.setState({appState: nextAppState});
      }
    
      render() {
        return (
          <Text>Current state is: {this.state.appState}</Text>
        );
      }
    }
    

    使用钩子

    import React, { useState, useEffect } from 'react';
    import { AppState, Text } from 'react-native';
    
    function AppStateExample() {
      const [appState, setAppState] = useState(AppState.currentState);
      var eventListenerSubscription;
    
      const _handleAppStateChange = nextAppState => {
        if (appState.match(/inactive|background/) && nextAppState === 'active') {
          console.log('App has come to the foreground!');
        }
        setAppState(nextAppState);
      };
    
      useEffect(() => {
        //on mount
        eventListenerSubscription = AppState.addEventListener('change', _handleAppStateChange);
        return () => {
          // on unmount
          eventListenerSubscription.remove();
        };
      }, []);
    
      return <Text>Current state is: {appState}</Text>;
    }
    export default AppStateExample;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2015-05-13
      • 1970-01-01
      • 1970-01-01
      • 2015-04-11
      • 2012-07-21
      相关资源
      最近更新 更多