Emi*_*ily 6 javascript location reactjs react-native
我对这个错误感到很难过,因为以前一切都工作得很好,现在它说它已被弃用。有人可以帮助我解决错误,因为它不起作用,我已经尝试了应用程序状态文档中的新代码,但仍然出现相同的错误TypeError: undefined is not an object (evaluating '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 });
};
Run Code Online (Sandbox Code Playgroud)
//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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3011 次 |
| 最近记录: |