PR already exists but probably needs to be rebased at this point: https://github.com/react-navigation/react-navigation/pull/10060
Currently there is no way to pass an
onPress
handler to
Drawer.Screen
. There are many reasons why someone would want to do this, and I've found several people looking for this functionality on stackoverflow. For me personally, some of my
Drawer.Screen
components are
Stack.Navigator
s, and I want to be able to reset the stack when the drawer item is pressed.
I am aware of the ability to pass your drawer navigator a custom
drawerContent
where you can then pass it
DrawerItem
s with
onPress
handlers, and I am already doing that for some things like my logout button. Trying to use that for screens seems pretty cumbersome though, since it seems like then you have to implement all of the navigation and focused logic as well as drawer open/close state. Additionally, you can only place them before or after all of your regular
Drawer.Screen
s that are rendered via
DrawerItemList
I solved this by adding an optional
onPress
that gets passed the normal
onPress
handler. That way the user can do what they want, and call the default
onPress
before or after (or not at all?)
My best guess as for why this functionality does not currently exist is that it can allow the user to do things that conflict with what react-navigation is doing internally when you tap on a drawer item, which could lead to bugs. If this is the case, maybe it could be called
dangerousOnPress
? That said, I have been using this without any issues.