【发布时间】:2016-06-05 19:12:38
【问题描述】:
我正在尝试从 componentDidMount(或渲染)函数获取 region 状态,但它失败了。你知道为什么吗?
这是我的代码:
constructor(props) {
super(props);
navigator.geolocation.getCurrentPosition(
(position) => {
this.region = {
latitude: position.coords.latitude,
latitudeDelta: 0.005,
longitude: position.coords.longitude,
longitudeDelta: 0.005,
}
console.log("location from constructor:", this.region);
},
(error) => alert(error.message),
{enableHighAccuracy: true, timeout: 20000, maximumAge: 2000}
);
this.state = {
region: this.region
}
}
componentDidMount() {
console.log("location from componentDidMount:", this.state.region);
}
这是我的日志:
location from componentDidMount: undefined
location from constructor: Object {latitude: 48.8732, latitudeDelta: 0.005, longitude: 2.402855, longitudeDelta: 0.005}
【问题讨论】:
标签: javascript react-native ecmascript-6 react-jsx