TypeError: undefined is not an object (evaluating 'appState.remove')
Asked Answered
F

2

6

enter image description hereim having a hard time with this error because everything used to work perfectly now its says that its deprecated. Can someone help me with error cause nothing its working, I have tried the new code in the App State documentation but still the same error TypeError: undefined is not an object (evaluating 'appState.remove')

This is my code:

constructor(props) {
    super(props);
    this.state = {
      region: null,
      status: undefined,
      appState: AppState?.currentState,
      modalVisible: true,
      isLoading: true,
    };
    this._getLocationAsync();
  }

  componentDidMount() {
    AppState.removeEventListener("change", this.handleAppStateChange);
  }
  handleAppStateChange = (nextAppState) => {
    if (
      this.state.appState.match(/inactive|background/) &&
      nextAppState === "active"
    ) {
      this._getLocationAsync();
    }
    this.setState({ appState: nextAppState });
  };

  componentWillUnmount() {
    AppState.addEventListener("change", this.handleAppStateChange);

    this._getLocationAsync();
  }
  _getLocationAsync = async () => {
    let { status } = await Location.requestPermissionsAsync();
    let location = await Location.getCurrentPositionAsync({
      enableHighAccuracy: false,
      timeout: 10000,
      maximumAge: 0,
    });

    let region = {
      latitude: location.coords.latitude,
      longitude: location.coords.longitude,
      latitudeDelta: 0.0045,
      longitudeDelta: 0.0045,
    };
    this.props.callback({
      latitude: region.latitude,
      longitude: region.longitude,
    });
    this.setState({ region: region, status: status });
  };
Foremost answered 12/10, 2021 at 13:22 Comment(2)
Can you show full error ? It seems to miss few code part. But appState is null (that what create the error)Tubercle
@Tubercle I have added a pic of errorForemost
I
5
//listen to AppState change in componentDidMount
componentDidMount() {
    this.subscriptionToAppState = AppState.addEventListener("change",  this.handleAppStateChange);
}

//stop listen 
componentWillUnmount() {
    //if your react native version 0.65 or more use this
    this.subscriptionToAppState.remove();
    //if your react native version min than v 0.65 use the deprecated methode like this 
    this.subscriptionToAppState.removeEventListener("change", this.handleAppStateChange);
}
Irremediable answered 12/10, 2021 at 14:15 Comment(1)
so this.subscriptionToApp is declared in constructor ? is it initialized as null?Pharyngitis
W
0

If we implement the following way , we can avoid this error.

import {
  AppState
} from 'react-native';

const appState = useRef(AppState.currentState);

 useEffect(() => {
        AppState.addEventListener('change', handleAppStateChange);
        return () => {
            console.log('unmount');
          AppState.removeEventListener('change', handleAppStateChange);
        };
      }, []);


    const handleAppStateChange = (nextAppState) => {
        console.log('App State: ' + nextAppState);
        if (appState.current != nextAppState) {
          if (appState.current.match(/inactive|background/) 
                && nextAppState === 'active') {
            console.log(
              'App State: ' +
              'App has come to the foreground!'
            );
          }
          appState.current = nextAppState;
        }
      };
Walleyed answered 2/10, 2022 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.