如何在反应原生 Es6 上检测 OnResume 和 onPause [英] How to detect OnResume and onPause on react native Es6

查看:48
本文介绍了如何在反应原生 Es6 上检测 OnResume 和 onPause的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I detect any method from background to foreground es6?

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

Is this possible in React-native? and is there any class or tool that could help with this type of method?

推荐答案

试试这个,它对我有用

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>
    );
  }

}

这篇关于如何在反应原生 Es6 上检测 OnResume 和 onPause的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆