我很想在header上调用函数右键单击屏幕。 我将func作为useEffect的参数传递,如下所示。
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'
答案 0 :(得分:4)
您不需要在标题上设置参数。您可以直接将该方法传递给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
}
Here是一些文档。