【发布时间】:2019-12-28 18:16:33
【问题描述】:
我在 React Native 中使用 navigator.geolocation API 将用户坐标设置为组件状态的一部分,MapView 应该用作 region 属性,并且运行 getCurrentPosition() 会抛出一个错误。
我不确定如何解决这个问题。错误表明这是一个不变性问题,尽管我确定我在应该使用的位置使用了 let 和 const。
这是我的初始状态:
this.state = {
data: {
name: "Nie powinieneś tego widzieć.",
address: "Wyślij zrzut ekranu tego widoku do nas na stronie dokosciola.pl w zakładce Kontakt.",
url: "https://dokosciola.pl",
coords: {
latitude: undefined,
longitude: undefined,
latitudeDelta: 0.00922 * 1.5,
longitudeDelta: 0.00421 * 1.5
},
hours: ["8:00", "10:00", "12:00"]
}
};
这就是我使用地理定位 API 的方式:
locateUser = () => {
navigator.geolocation.getCurrentPosition(
position => {
let state = this.state;
state.data.coords.latitude = position.coords.latitude;
state.data.coords.longitude = position.coords.longitude;
this.setState(state);
console.log(this.state);
},
error => Alert.alert("Ups!", `Wystąpił wewnętrzny błąd:\n${error}`),
{ enableHighAccuracy: true, timeout: 30000, maximumAge: 5000 }
);
};
我在安装应用程序之前运行locateUser() 函数,所以:
componentWillMount() {
this.locateUser();
}
这就是我使用react-native-maps 中的MapView 组件的方式:
<View style={{ flex: 1 }}>
<MapView
style={{ flex: 1 }}
initialRegion={this.state.data.coords}
region={this.state.data.coords}
mapType={"mutedStandard"}
>
{data.map(element => {
return (
<ChurchMarker
coords={element.coords}
key={element.id}
onPress={() => this.update(element)}
/>
);
})}
</MapView>
</View>
ChurchMarker 是预煮的Marker,也来自react-native-maps 和data - 一个简单的对象数组,模拟潜在的 API 响应:
[
{
id: string,
name: string,
address: string,
url: string,
coords: {
latitude: number,
longitude: number
},
hours: string[]
},
...
]
我希望MapView 在安装应用程序时关注用户坐标,但我在locateUser() 中指定的错误会执行,并显示以下消息:
Error: you attempted to set the key `latitude` with the value `37.375834` on an object that is meant to be immutable and has been frozen.
之后还有一个警告:
Warning: Failed prop type: The prop `region.latitude` is marked as required in `MapView`, but its value is `undefined`.
经度也一样。这意味着状态没有更新。 对此有任何修复吗?我在执行什么错误?
【问题讨论】:
-
提醒:有不可变的对象吗?
-
@zixuan 没有,我没有在任何地方使用任何
consts。我用了setState(),没有直接修改状态。一切似乎都很好。 -
好的,API 呢?您将
state定义为this.state,其中必须使用setState(),然后您直接将state的经纬度更改为经纬度。 -
@zixuan 你的意思是我应该直接用
position坐标做setState,而不用let state = this.state?这不是您通常更新状态的方式吗?通过挑选我要修改的字段? -
我建议你尝试用对象的方式更新
this.state。
标签: reactjs react-native geolocation state immutability