【问题标题】:React-Native : change state when app is in backgroundReact-Native :当应用程序处于后台时更改状态
【发布时间】:2021-07-26 14:04:55
【问题描述】:

我创建了一个使用 React Native AppState 的简单应用程序:

import React, {Component} from 'react'
import {AppState, Text , View} from 'react-native'


export default  class AppStateExample extends  React.Component {

  constructor(props){
    super(props);
    this.state = {
      name:'not change'
    }
  }

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

  componentWillUnmount() {
    AppState.removeEventListener('change', this._handleAppStateChange);
  }

  _handleAppStateChange = (nextAppState) => {

    if(AppState.currentState=='background'){
      console.log('background mode');
      this.setState({name:'back'});
    }
    if(AppState.currentState =='active'){
      //...
    }
  };


  render() {
    return (
      <View>
        <Text>State Name : {this.state.name}</Text>
      </View>
    );
  }

}

当我尝试将应用程序从前台切换到后台,然后从后台切换到前台时,console.log('background mode'); 工作得很好并且控制台 打印'background mode'

但是

this.setState({name:'back'}); 不起作用,我看到 'not change' 文本显示在视图中

【问题讨论】:

  • 您找到this.setState 不起作用的任何原因吗?就我而言,setState 根本不存在,这很奇怪。

标签: react-native react-native-android


【解决方案1】:

实际上,基于React Native Docs 对AppState 功能组件的更改,我更喜欢编写如下代码:

import { useRef, useState, useEffect } from "react";
import { AppState } from "react-native";

const AppStateManager = () => {
  const appState = useRef(AppState.currentState);
  const [appStateVisible, setAppStateVisible] = useState(appState.current);

  useEffect(() => {
    AppState.addEventListener("change", handleAppStateChange);

    return () => {
      AppState.removeEventListener("change", handleAppStateChange);
    };
  }, []);

  const handleAppStateChange = (nextAppState) => {
    if (
      appState.current.match(/inactive|background/) &&
      nextAppState === "active"
    ) {
      console.log("App has come to the foreground!");
    }

    appState.current = nextAppState;
    setAppStateVisible(appState.current);
    console.log("AppState", appState.current);
  };

  return null;
};

export default AppStateManager;

当然,我们可以在项目的根目录中使用这个组件,就像 React 组件一样:

~~~
  <App>
    ~~
    <AppStateManager />
    ~~
.
.
.

【讨论】:

    【解决方案2】:

    这是因为,AppState.addEventListener('change', this._handleAppStateChange); 注册太晚了。 在加载主要组件并可能通过状态管理库传递值之前,您可能希望在应用程序中首先监听 AppState

    【讨论】:

    【解决方案3】:

    我会选择一个包含所有端点的开关 注意:获取appState状态AppState.currentState

    this.state = {
      appState: AppState.currentState
      // https://facebook.github.io/react-native/docs/appstate.html
    };
    
    
    componentWillMount() {
      AppState.addEventListener('change', () => this._handleAppStateChange());
    };
    
    
    componentWillUnmount() {
      AppState.removeEventListener('change', () => this._handleAppStateChange());
    }
    
    _handleAppStateChange() {
      // https://facebook.github.io/react-native/docs/appstate.html
      const {
        appState
      } = this.state;
      console.warn({
        appState
      })
    
      this.fetchData().catch(error => error);
    
      switch (appState) {
        case 'active': //The app is running in the foreground
          this.onStart();
          break;
        case 'background': // The app is running in the background. The user is either
          this.onEnd();
          console.warn('background');
          break;
    
        case 'inactive':
          // The app transitioning between foreground & background or entering the Multitasking view or in the event of an incoming call
          console.warn('Inactive');
          break;
    
        default:
          console.warn('_handleAppStateChange default');
      }
    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 2021-11-28
      相关资源
      最近更新 更多