React-navigation v5:将函数作为参数传递给 headerRight 按钮 [英] React-navigation v5 : Pass function as parameter to be used in headerRight button
本文介绍了React-navigation v5:将函数作为参数传递给 headerRight 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在标题上调用函数右键单击屏幕.我在 useEffect 中将 func 作为参数传递,如下所示.
I ham trying to call function on headerRight button click on screen. I am passing func as param in useEffect as shown below.
useEffect(() => {
navigation.setParams({ _confirmClick: confirmNotification })
}, [navigation])
useLayoutEffect(() => {
navigation.setOptions({
headerRight: () => (
<TouchableOpacity
onPress={() => params._confirmClick('New') }
style={[theme.marginRight15]}>
<View style={[styles.sendNotificationButton,
{
backgroundColor: notification ? theme.colors.notificationDeleteButtonColor :
theme.colors.sendbuttonColor,
borderColor: notification ? theme.colors.notificationDeleteButtonColor :
theme.colors.sendbuttonColor,
}]}>
<Ionicons name="ios-send" size={15}
style={theme.padding3}
color={theme.colors.whiteColor} />
</View>
</TouchableOpacity>
),
});
}, [navigation]);
function confirmNotification(status) {
...
}
当我点击按钮时,它说:TypeError:无法读取未定义的属性_confirmClick"
When I click on button it says : TypeError: Cannot read property '_confirmClick' of undefined
推荐答案
你不需要在 header 上设置参数.您可以直接将该方法传递给 headerRight :
You don't need to set parameter on header. You can directly pass that method to headerRight :
function yourScreenName({ navigation }) {
useLayoutEffect(() => {
navigation.setOptions({
headerRight: () => (
<TouchableOpacity
onPress={() => confirmNotification('New')}
style={[theme.marginRight15]}>
<View style={[styles.sendNotificationButton,
{
backgroundColor: notification ? theme.colors.notificationDeleteButtonColor :
theme.colors.sendbuttonColor,
borderColor: notification ? theme.colors.notificationDeleteButtonColor :
theme.colors.sendbuttonColor,
}]}>
<Ionicons name="ios-send" size={15}
style={theme.padding3}
color={theme.colors.whiteColor} />
</View>
</TouchableOpacity>
),
});
}, [navigation, confirmNotification]); // pass method directly here
}
这里是一些文档.
这篇关于React-navigation v5:将函数作为参数传递给 headerRight 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文