javascript - TypeError:未定义不是对象(评估“appState.remove”)
问题描述
我很难解决这个错误,因为现在一切都可以正常工作,它说它已被弃用。有人可以帮我解决错误,因为它没有任何工作,我已经尝试了 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 });
};
解决方案
//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);
}
推荐阅读
- firebase - 在 Firebase Cloud Messaging 中,我在哪里可以看到已注册推送的用户/设备列表?
- python - TypeError:对象无法转换为 IntegerDtype
- java - JDBC 模板为空
- java - Hadoop 1.2.1 - mapreduce 编译期间的“java.lang.NoClassDefFoundError: org/apache/hadoop/mapred/JobConf”
- python - 将由 RF 制作的子词典与从 python 中的函数获取的词典进行比较
- html - 删除或制作表格的透明背景和标题颜色
- javascript - Angular 9 单元测试外部函数引用服务内的属性给出错误
- javascript - JavaScript 中的赋值是如何工作的?
- ssl - 如何在 tls 握手期间验证客户端证书
- javascript - 半高分屏,半边可滚动