0
나는 사용자의 현재 위치를 얻기 위해 이러한 패턴을 사용합니다. 핵심 문제는 위치를 얻는 데 약간의 지연이 있으며 콘솔은 없이 undefined
을 던졌습니다. 나는 그것을 해결하기 위해 몇 가지 다른 방법을 시도했지만 모두 실패 :componentDidMount, getCurrentPosition 지연
navigator.geolocation.getCurrentPosition(function(location) {
this.setState({
lat: location.coords.latitude,
lon: location.coords.longitude
})
}).bind(this);
예를 들면 겸손 let _this = this
을. 제발, setState
에 더 현명한 방법으로, geolocation에 따라?
가능한 중복 https://stackoverflow.com/questions/41278385/updated-state-not-을 reflection-after-setstate/41278440 # 41278440 –
'navigator.geolocation.getCurrentPosition (function (location) { this.setState ({ lat : location.coords.})에서'.bind (this)'의 위치를 약간 수정하십시오. 위도, lon : location.coords.longitude ) } .bind (this));'콜백 함수를 getCurrentPosi에 바인드해야하므로 그것은 작동 할 것이다. 또한 setState는 asyc이므로 setState 이후의 console.log()는 정확한 결과를 제공하지 못합니다. 중복 된 질문 –
@Shubham Khatri가 정확한지 확인하십시오. bind 대신 arrow 함수를 사용할 수도 있습니다. –