gpt4 book ai didi

javascript - 类型错误 : this is undefined on setstate reactjs

转载 作者:行者123 更新时间:2023-11-30 19:22:46 26 4
gpt4 key购买 nike

<分区>

任何人都可以帮助我或给我解决方案以获得值(value)

“位置.坐标.纬度”

我希望在用户单击按钮后该值更新 this.state "city"

class Home extends Component{

constructor(props){
super(props)
this.state = {
city: '',
lats : '',
long : ''
}
this.Kirim = this.Kirim.bind(this)
}

Kirim() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(Position){
this.setState({ city:position.coords.latitude}) // erorr here
})

} else {

}
}

render(){
let {city} = this.state
console.log(city)
return(

<Fragment>
<div className="form-add">
<button onClick={this.Kirim}>Kirim</button>
</div>
</Fragment>

)
}

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com