我如何将函数传递给导航(堆栈)组件
所以我想将savedLocationHandler()函数传递给导航组件,这样我就可以将它放在标题按钮上,因为我猜这是访问标题的唯一方法,但我不知道该怎么做!我尝试了 setParams 但它不起作用,我只知道路由方法,但我猜这只能从一个屏幕到另一个屏幕使用?!
import React, { useCallback, useEffect, useState } from "react";
import { Button, StyleSheet, Text, View } from "react-native";
import MapView, { Marker } from "react-native-maps";
const MapScreen = ({ navigation }) => {
const [selectedLocation, setSelectedLocation] = useState();
const MapRegion = {
latitude: 37.78,
longitude: -122.43,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
};
const selectLocationHandler = (event) => {
console.log(event);
setSelectedLocation({
lat: event.nativeEvent.coordinate.latitude,
lng: event.nativeEvent.coordinate.longitude,
});
};
let markerCoordinates;
const savedLocationHandler = () => {
if (!selectedLocation) {
return;
}
navigation.navigate("NewPlaceScreen", {
selectedLocation: selectedLocation,
});
};
if (selectedLocation) {
markerCoordinates = {
latitude: selectedLocation.lat,
longitude: selectedLocation.lng,
};
}
return (
<MapView
style={styles.mapImage}
region={MapRegion}
onPress={selectLocationHandler}
>
{markerCoordinates && (
<Marker title="Picked Location" coordinate={markerCoordinates}></Marker>
)}
</MapView>
);
};
这是导航组件(仅在我想传递函数的地方)
<Stack.Screen
name="MapScreen"
component={MapScreen}
options={({ navigation }) => ({
headerRight: () => (
<TouchableOpacity
onPress={() => { {/* i want to pass the function here*/ }
saveFn;
}}
style={{ marginHorizontal: 20 }}
>
<Text
style={{
fontSize: 19,
fontWeight: "bold",
color: Platform.OS === "android" ? "#C06E00" : "white",
}}
>
Save
</Text>
</TouchableOpacity>
),
})}
/>
</Stack.Navigator>
so I wanted to pass the savedLocationHandler() function to the navigation component so I can put it on the Header button because i guess that's the only way to access the header but I didn't know how to do that ! i tried setParams and it didn't work , I only know the route method but that's only used from a screen to another i guess ?!
import React, { useCallback, useEffect, useState } from "react";
import { Button, StyleSheet, Text, View } from "react-native";
import MapView, { Marker } from "react-native-maps";
const MapScreen = ({ navigation }) => {
const [selectedLocation, setSelectedLocation] = useState();
const MapRegion = {
latitude: 37.78,
longitude: -122.43,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
};
const selectLocationHandler = (event) => {
console.log(event);
setSelectedLocation({
lat: event.nativeEvent.coordinate.latitude,
lng: event.nativeEvent.coordinate.longitude,
});
};
let markerCoordinates;
const savedLocationHandler = () => {
if (!selectedLocation) {
return;
}
navigation.navigate("NewPlaceScreen", {
selectedLocation: selectedLocation,
});
};
if (selectedLocation) {
markerCoordinates = {
latitude: selectedLocation.lat,
longitude: selectedLocation.lng,
};
}
return (
<MapView
style={styles.mapImage}
region={MapRegion}
onPress={selectLocationHandler}
>
{markerCoordinates && (
<Marker title="Picked Location" coordinate={markerCoordinates}></Marker>
)}
</MapView>
);
};
and this is the navigation component ( only where i want to pass the function )
<Stack.Screen
name="MapScreen"
component={MapScreen}
options={({ navigation }) => ({
headerRight: () => (
<TouchableOpacity
onPress={() => { {/* i want to pass the function here*/ }
saveFn;
}}
style={{ marginHorizontal: 20 }}
>
<Text
style={{
fontSize: 19,
fontWeight: "bold",
color: Platform.OS === "android" ? "#C06E00" : "white",
}}
>
Save
</Text>
</TouchableOpacity>
),
})}
/>
</Stack.Navigator>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以使用
useLayoutEffect
为任何屏幕的headerRight
属性提供一个函数,如下所示。We could provide a function for the
headerRight
prop for any screen using theuseLayoutEffect
as follows.