Reactjs 方法未在react native中触发
我是react native的新手,所以请帮我解决这个问题 假设我有两个屏幕。屏幕A和屏幕B。在屏幕A中,每隔20秒使用Reactjs 方法未在react native中触发,reactjs,react-native,react-navigation,Reactjs,React Native,React Navigation,我是react native的新手,所以请帮我解决这个问题 假设我有两个屏幕。屏幕A和屏幕B。在屏幕A中,每隔20秒使用Geolocation.watchPosition()获取一个位置。当我导航到屏幕B时,仍在提取位置。假设我在redux中更新了一个值,并且位置服务停止了。稍后我会更新redux值,以便可以恢复位置服务。redux值已更新,但位置服务未恢复。请记住,那时我可能在B屏 componentWillReceiveProps(nextProps, nextState) { if (ne
Geolocation.watchPosition()
获取一个位置。当我导航到屏幕B时,仍在提取位置。假设我在redux中更新了一个值,并且位置服务停止了。稍后我会更新redux值,以便可以恢复位置服务。redux值已更新,但位置服务未恢复。请记住,那时我可能在B屏
componentWillReceiveProps(nextProps, nextState) {
if (nextProps.updateServices != this.props.updateServices) {
if ( this.props.updateServices === Y) {
__DEV__ && console.log("HERE HERE");
this.gpsInterval = setInterval(() => {
Geolocation.getCurrentPosition(info => {
const latitude = info.coords.latitude;
const longitude = info.coords.longitude;
RouteData.latitude = latitude;
RouteData.longitude = longitude;
this.props.updateUserLocation({ latitude, longitude });
this.setState(
{
latitude: latitude,
longitude: longitude,
latitudeDelta: 0.005,
longitudeDelta: 0.01,
showLocationStatus: false
},
() => {
this.gotLocation = true;
clearInterval(this.gpsInterval);
if (!this.venueDone) this.getAllVenues(latitude, longitude);
}
);
});
}, 3000);
}
}
}
这就是我如何尝试从屏幕B更新屏幕A中的位置服务。
我在此处获取日志,
,但未检测到位置更改
编辑1
屏幕A
class HomeMapView extends React.Component {
constructor(props) {
super(props);
this.state = {
showLocationStatus: true
};
}
getCurrentPosition = (currentLatitude, currentLongitude) => {
console.log("Lat: " + currentLatitude + " Long: " + currentLongitude);
RouteData.latitude = currentLatitude;
RouteData.longitude = currentLongitude;
this.props.updateGpsPosition({
latitude: currentLatitude,
longitude: currentLongitude
});
// if (!this.venueDone) {
// this.setState(
// {
// latitude: currentLatitude,
// longitude: currentLongitude,
// latitudeDelta: 0.005,
// longitudeDelta: 0.01
// },
// () => {
// this.getAllVenues(currentLatitude, currentLongitude);
// }
// );
// }
};
enableLocationService() {
console.log("REACHED HERE");
this.locationListener = Geolocation.watchPosition(
position => {
__DEV__ && console.log("LOCATION CHANGED");
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
this.props.updateUserLocation({
latitude,
longitude
});
this.getCurrentPosition(latitude, longitude);
},
error => {
console.log("error" + JSON.stringify(error));
RNAndroidLocationEnabler.promptForEnableLocationIfNeeded({
interval: 20000,
fastInterval: 50000
})
.then(data => {
showAlert(
"FETCHING ...",
"Getting Data Please wait a moment",
"success");
onCurrentLocationClicked(this.getCurrentPosition);
})
.catch(err => {
showAlert(
"GPS Failed",
"Problem in enabling GPS please enable gps",
"danger");
});
}, {
fastestInterval: 10000,
interval: 10000,
enableHighAccuracy: true,
distanceFilter: 0
});
}
disableLocationService() {
Geolocation.clearWatch(this.locationListener);
}
componentDidMount() {
this.gpsInterval = setInterval(() => {
Geolocation.getCurrentPosition(info => {
const latitude = info.coords.latitude;
const longitude = info.coords.longitude;
RouteData.latitude = latitude;
RouteData.longitude = longitude;
this.props.updateUserLocation({
latitude,
longitude
});
this.setState({
latitude: latitude,
longitude: longitude,
latitudeDelta: 0.005,
longitudeDelta: 0.01,
showLocationStatus: false
},
() => {
this.gotLocation = true;
clearInterval(this.gpsInterval);
if (!this.venueDone)
this.getAllVenues(latitude, longitude);
});
});
}, 3000);
}
componentWillReceiveProps(nextProps, nextState) {
if (nextProps.locationService != this.props.locationService) {
if (nextProps.locationService === "Y") {
__DEV__ && console.log("LOCATION SERVICE ENABLED");
this.enableLocationService();
} else if (nextProps.locationService === "N") {
__DEV__ && console.log("LOCATION SERVICE DISABLED");
this.disableLocationService();
}
}
//added to enable location services after exit venue
if (nextProps.enterVenue != this.props.enterVenue) {
if (!nextProps.enterVenue && this.props.locationService === Y) {
__DEV__ && console.log("HERE HERE");
this.gpsInterval = setInterval(() => {
Geolocation.getCurrentPosition(info => {
const latitude = info.coords.latitude;
const longitude = info.coords.longitude;
RouteData.latitude = latitude;
RouteData.longitude = longitude;
this.props.updateUserLocation({
latitude,
longitude
});
this.setState({
latitude: latitude,
longitude: longitude,
latitudeDelta: 0.005,
longitudeDelta: 0.01,
showLocationStatus: false
},
() => {
this.gotLocation = true;
clearInterval(this.gpsInterval);
if (!this.venueDone)
this.getAllVenues(latitude, longitude);
});
});
}, 3000);
}
}
}
}
const mapStateToProps = state => ({
locationService: state.settings.locationService,
enterVenue: state.venues.enterVenues
});
const mapDispatchToProps = dispatch => ({
upateLocationService: data => dispatch(upateLocationService(data)),
});
export default connect(
mapStateToProps,
mapDispatchToProps)(HomeMapView);
感谢您的帮助
提前感谢。如果您在updateUserLocation中记录新的lat/lng数据,您看到了吗?是的,我看到了日志您在两个屏幕中从redux读取数据吗(
导出默认连接(MapStateTrops,actions)(YourCompName);
+组件将接收props
当您使用操作更改redux状态时,您需要使用连接重新读取redux返回的状态(MapStateTrops,null/actions)
谁将更改当前组件的道具,您将获得更新的道具,其中组件将接收道具
数据将在屏幕A中访问,当数据从两个屏幕更新时