【发布时间】:2021-09-28 03:37:54
【问题描述】:
我有两个组件 Place 和 Map。每次当组件Place 中的用户单击“在地图上显示”按钮时,我打开Map 并从Place 获取路线参数纬度/经度,我想在标记附近渲染区域。我尝试将当前纬度/经度设置为状态以在标记附近重新渲染组件,但它不起作用,我的地图在我关闭它的同一个地方打开。我发现的唯一选择是在关闭Map 时卸载组件,但我认为这不是最好的解决方案。
地点:
const onNavigationTap = () => {
navigation.navigate('Map', {
destination: data["data"].coordinates
});
}
return(
<TouchableOpacity onPress={() => onNavigationTap()}>
<View style={{flexDirection: "row", alignItems: "center"}}>
<Ionicons size={hp('5%')} name={'navigate-circle-outline'} color='white'/>
</View>
</TouchableOpacity>
)
地图:
const [currentDestination, setCurrentDestination] = useState(undefined);
if (route.params) {
var {destination} = route.params;
}
useEffect(() => {
setCurrentDestination(destination);
}, [destination]);
return (
<MapView
mapType={satellite ? "hybrid" : "standard"}
style={{flex: 1}}
showsUserLocation={true}
followsUserLocation={true}
provider={PROVIDER_GOOGLE}
initialRegion={
currentDestination ?
{
longitude: parseFloat(currentDestination.longitude),
latitude: parseFloat(currentDestination.latitude),
longitudeDelta: 0.0043,
latitudeDelta: 0.0034,
} : {
latitude: 53.227200,
longitude: 50.243698,
latitudeDelta: 3,
longitudeDelta: 3,
}
}
>
</MapView>
)
【问题讨论】:
标签: react-native use-effect use-state