我如何在componentDidMount的watchID中访问例如区域对象或setState?在setState之后,componentDidMount中的this.state.error输出为false,但在render中为true。 如何访问区域对象?
constructor (props) {
super(props);
this.state = {
latitude: 0,
longitude: 0,
error: false
};
}
componentDidMount () {
watchID = Geolocation.watchPosition((position) => {
let region = {
lat: position.coords.latitude,
long: position.coords.longitude
};
this.setState({
latitude: region.lat || this.state.latitude,
longitude: region.long || this.state.longitude });
},
(error) => {
this.setState({ error: true });
});
}
componentWillUnmount () {
navigator.geolocation.clearWatch(watchID);
}