how to conditionally prevent navigation in react-native with react-navigation 3

Viewed 2974

In my react native app I have form screens in which users have to fill-in some details. If a user started filling a form and wants to navigate away I want to warn "you will lose your details....". The problem is that navigation can be from many different places - drawer, tabs etc.

I know I can use componentWillBlur to catch the moment before leaving, but I couldn't find a way to actually cancel the navigation in this method.

1 Answers

With react-navigation you can check the current screen that is in view right now so that you can check your condition before navigating. Get the current route like this: this.props.navigation.state.routeName For more info go to the documentation: https://reactnavigation.org/docs/en/navigation-prop.html

Related