React Native:装入组件时会触发AppState侦听器中的活动状态

问题描述 投票:0回答:3

[我已经在React Native中为AppState设置了一个侦听器,以了解我的应用何时从后台切换到前台,以便显示初始屏幕。

useEffect(() => {
AppState.addEventListener('change', handleAppStateChange);
},[])

const handleAppStateChange = newState => {

    if (newState === 'active') {
      RNBootSplash.show();
    }

  };

问题是,这仅在我编译发行版APK时发生,由于某种原因,它在调试中工作正常,每次安装组件时都会显示启动屏幕,而仅当应用程序切换到活动状态时才触发从背景状态。有帮助吗?

使用React Native 61.5

android reactjs react-native
3个回答
0
投票

尝试这种方式:

const [appState, setAppState] = React.useState(AppState.currentState)

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

    setAppState(AppState.currentState)

    return AppState.removeEventListener('change')
},[])

const handleAppStateChange = newState => {
    if (appState.match(/inactive|background/) && newState === 'active') {
      console.log('App has come to the foreground!')
      // Show the splash screen
      RNBootSplash.show()
    } else {
      console.log('App has gone to the background!')
      // do something in background
    }

    setAppState(newState)
}

0
投票

请!我希望您使用Payjoy应用程序解锁我的所有应用程序。因为不允许在此zte选项卡上停止payjoy应用程序。请帮助解决此问题。


0
投票

仅起作用的是使用引用

const latestAppState = React.useRef(AppState.currentState)

const handleAppStateChange = newState => {

    if (latestAppState.current.match(/inactive|background/) && newState === 'active') {
      RNBootSplash.show();

    }
    latestAppState.current = newState
  }
© www.soinside.com 2019 - 2024. All rights reserved.