TypeError:未定义不是对象(评估“appState.remove”)

Posted

技术标签:

【中文标题】TypeError:未定义不是对象(评估“appState.remove”)【英文标题】:TypeError: undefined is not an object (evaluating 'appState.remove') 【发布时间】:2021-12-01 02:23:05 【问题描述】:

im 很难解决这个错误,因为过去一切正常,现在它说它已被弃用。有人可以帮我解决错误,因为它没有任何工作,我已经尝试了 App State 文档中的新代码,但仍然是相同的错误 TypeError: undefined is not an object (evalating 'appState.remove')

这是我的代码:

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 );
  ;

【问题讨论】:

你能显示完整的错误吗?它似乎错过了一些代码部分。但是appState 是空的(那是什么造成了错误) @Elikill58 我添加了一张错误图片 【参考方案1】:
//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);

【讨论】:

以上是关于TypeError:未定义不是对象(评估“appState.remove”)的主要内容,如果未能解决你的问题,请参考以下文章

TypeError:未定义不是对象(评估'_ref.state)

TypeError:未定义不是对象(评估'array.length')反应原生[关闭]

TypeError:未定义不是一个对象(评估'details.date.getTime')

TypeError:未定义不是对象(评估“appState.remove”)

TypeError:未定义不是对象(评估“window.location.href”)

如何在本机反应中修复“TypeError:未定义不是对象(评估'_reactNativeCamera.default.constants')”错误?