Using React Hooks to update location give error: Error while updating 'region' of a view managed by: AIRMAP

Using React Hooks to update location give error: Error while updating 'region' of a view managed by: AIRMAP

我有这个错误,我不知道哪里出了问题。我正在使用 React Hooks(useState 和 useEffect)来更新用户在 React Native 应用程序中的当前位置。

import React, {useEffect, useState} from 'react';
import MapView, {PROVIDER_GOOGLE} from 'react-native-maps';
import Geolocation from '@react-native-community/geolocation';

const MapScreen = () => {
   const [location, setLocation] = useState({
latitude: 18.9712,
longitude: -72.2852,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
 });

useEffect(() => {
Geolocation.getCurrentPosition(position => {
  const region = {
    latitude: position.coords.latitude,
    longitude: position.coords.longitude,
    latitudeDelta: 0.001,
    longitudeDelta: 0.001,
  }
  setLocation({region})
})

}, [])
return (
<MapView
  provider={PROVIDER_GOOGLE}
  style={{flex: 1}}
  showsUserLocation={true}
  region={location}

/>
)
}

export default MapScreen

我认为问题出在这里,

setLocation({region})

这里region是一个对象,所以就变成了

setLocation({{
    latitude: position.coords.latitude,
    longitude: position.coords.longitude,
    latitudeDelta: 0.001,
    longitudeDelta: 0.001,
}})

setState 不正确。

你只需要设置地区,

setLocation(region)