Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: undefined is not an object (evaluating 'appState.remove') #17

Open
heluansantos-zz opened this issue Nov 23, 2021 · 2 comments

Comments

@heluansantos-zz
Copy link

node_modules/react-native-appstate-hook/dist/index.js

`import { useState, useEffect } from 'react';
import { AppState } from 'react-native';

export default function useAppState(settings) {
const { onChange, onForeground, onBackground } = settings || {};
const [appState, setAppState] = useState(AppState.currentState);

useEffect(() => {
function handleAppStateChange(nextAppState) {
if (nextAppState === 'active' && appState !== 'active') {
isValidFunction(onForeground) && onForeground();
} else if (appState === 'active' && nextAppState.match(/inactive|background/)) {
isValidFunction(onBackground) && onBackground();
}
setAppState(nextAppState);
isValidFunction(onChange) && onChange(nextAppState);
}
const appState = AppState.addEventListener('change', handleAppStateChange);

// removing -> return appState.remove();
return;
}, [onChange, onForeground, onBackground, appState]);

// settings validation
function isValidFunction(func) {
return func && typeof func === 'function';
}
return { appState };
}`

Interim solution!

@imhazige
Copy link

imhazige commented Jan 3, 2022

Same problem occur after upgrade to react native 0.64

0.64 have a small difference, The solution is create the hook by ourself as it is simple, :).

@andrewastrachan
Copy link

We're on React Native 0.64.3 (Expo), and it looks like AppState.addEventListener is not returning an EventSubscription. It looks like that first appears in RN 0.65. In the meantime, we're doing this in a custom hook:

function isValidFunction(func) {
  return func && typeof func === "function";
}

export const useAppState = (settings) => {
  const { onChange, onForeground, onBackground } = settings || {};
  const [appState, setAppState] = useState(AppState.currentState);

  useEffect(() => {
    const handleAppStateChange = (nextAppState) => {
      if (nextAppState === "active" && appState !== "active") {
        isValidFunction(onForeground) && onForeground();
      } else if (
        appState === "active" &&
        nextAppState.match(/inactive|background/)
      ) {
        isValidFunction(onBackground) && onBackground();
      }
      setAppState(nextAppState);
      isValidFunction(onChange) && onChange(nextAppState);
    }
    AppState.addEventListener("change", handleAppStateChange);

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

  return { appState };
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants