javascript - TypeError: undefined is not an object (evaluating 'appState.remove') - Stack Overflow

im having a hard time with this error because everything used to work perfectly now its says that its d

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

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

  ponentWillUnmount() {
    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 });
  };

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

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

  ponentWillUnmount() {
    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 });
  };
Share Improve this question edited Oct 12, 2021 at 13:35 Emily asked Oct 12, 2021 at 13:22 EmilyEmily 1614 silver badges18 bronze badges 2
  • Can you show full error ? It seems to miss few code part. But appState is null (that what create the error) – Elikill58 Commented Oct 12, 2021 at 13:23
  • @Elikill58 I have added a pic of error – Emily Commented Oct 12, 2021 at 13:42
Add a ment  | 

2 Answers 2

Reset to default 5
//listen to AppState change in ponentDidMount
ponentDidMount() {
    this.subscriptionToAppState = AppState.addEventListener("change",  this.handleAppStateChange);
}

//stop listen 
ponentWillUnmount() {
    //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);
}

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 e to the foreground!'
            );
          }
          appState.current = nextAppState;
        }
      };

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745376610a4625028.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信