如何访问变量以响应本机对象

时间:2019-03-01 12:04:15

标签: react-native

我如何在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);
}

0 个答案:

没有答案