Reactjs 如果没有上下文错误,则无法定义ES6箭头函数
我是个新手,在这门课上我遇到了一个我似乎无法解决的问题。这是我的班级:Reactjs 如果没有上下文错误,则无法定义ES6箭头函数,reactjs,react-native,Reactjs,React Native,我是个新手,在这门课上我遇到了一个我似乎无法解决的问题。这是我的班级: constructor (props) { super(props) this.state = { mapRegion: null, hasLocationPermissions: false, locationResult: null } }; componentDidMount() { this._getLocationA
constructor (props) {
super(props)
this.state = {
mapRegion: null,
hasLocationPermissions: false,
locationResult: null
}
};
componentDidMount() {
this._getLocationAsync();
};
_handleMapRegionChange (mapRegion) {
console.log(mapRegion);
this.setState({ mapRegion });
};
_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
locationResult: 'Permission to access location was denied',
});
} else {
this.setState({ hasLocationPermissions: true });
}
let location = await Location.getCurrentPositionAsync({});
this.setState({ locationResult: JSON.stringify(location) });
// Center the map on the location we just fetched.
this.setState({mapRegion: { latitude: location.coords.latitude, longitude: location.coords.longitude, latitudeDelta: 0.0922, longitudeDelta: 0.0421 }});
}
render (){
return(
//..render here
)}}
最初,我试图使用ES6箭头来实现_handleMapRegionChange,但出现了一个语法错误,我无法完全理解,有什么帮助吗?
编辑:我得到的错误是
TypeError:this.setState不是函数。(在'this.setState({mapRegion:mapRegion}')中,'this.setState'未定义)
对此,我尝试将\u handleMapRegionChange
更改为匿名函数,如下所示:
\u handleMapRegionChange=mapRegion=>{
console.log(mapRegion);
this.setState({mapRegion});
};
但这样做会导致语法错误:
分析错误:意外标记=
我不太清楚我遗漏了什么。在_getLocationAsync方法中使用Try-catch方法来处理错误,您可以在catch()中控制台记录错误
\u getLocationAsync=async()=>{
试一试{
让{status}=wait Permissions.askAsync(Permissions.LOCATION);
如果(状态!=“已授予”){
这是我的国家({
locationResult:'访问位置的权限被拒绝',
});
}否则{
this.setState({hasLocationPermissions:true});
}
let location=await location.getCurrentPositionAsync({});
this.setState({locationResult:JSON.stringify(location)});
//把地图放在我们刚拿到的位置的中心。
这是我的国家({
地图区域:{
纬度:location.coords.latitude,
经度:location.coords.longitude,
纬度德尔塔:0.0922,
纵向德尔塔:0.0421,
},
});
}捕获(错误){
console.log(错误)
}
};代码>欢迎来到SO!当您遇到语法错误时,准确地分享它所说的内容以及它所指向的行是很有帮助的。感谢并对错误的格式表示歉意。我正试图修复一个似乎是范围错误的问题,但似乎无法正确使用语法将\u handleMapRegionchange
转换为一个匿名函数,类似于“\u getLocationAsync”。我最终试图修复的错误是:TypeError:this.setState不是函数。(在'this.setState({mapRegion:mapRegion}')中,'this.setState'未定义)
请编辑您的帖子以显示其他信息。