请问我的反应类,我试图更新我的状态,但我得到这个错误“无法读取属性setState的未定义”..我已经尝试每一个解决方案在线,但没有运气'this.setState'有什么问题
这里是代码
export default class PageBackground extends Component{
constructor(props) {
super(props);
this.state={
lat :'nothing',
longi :''
};
this.getLocation=this.getLocation.bind(this);
}
componentWillMount() {
this.getLocation();
}
getLocation() {
fetch('http://freegeoip.net/json/')
.then((res)=>res.json())
.then(function (objec){this.setState({lat:objec.latitude,longi:objec.longitude});})
.catch((err)=>console.log("didn't connect to App",err))
console.log(this.state.lat)
}
render(){
return(
<p>{this.state.lat}</p>
)
}
}
您是否获得控制台日志? – buoyantair
我认为'this'是在函数的上下文中。也许你应该尝试'that = this'和'that.setState' – AnkitG
有litterally一百万重复这个非常错误。我很惊讶你没能找到他们中的任何一个。 – Chris