The useBlocker hook allows you to block navigation when certain conditions are met, such as when a user has unsaved changes in a form. This provides a way to prompt users before they accidentally leave a page with unsaved work.
For more control, pass a function that receives location information:
The function receives:
currentLocation - The current pathname and search paramsnextLocation - Where the user is trying to navigatehistoryAction - The type of navigation ('push', 'pop', or 'replace')The blocker returns an object with a state property:
'unblocked'Navigation is not blocked. The blocker object has no additional properties.
'blocked'Navigation has been blocked and is waiting for user decision.
'proceeding'The user has chosen to proceed and navigation is in progress.
On web, useBlocker intercepts:
On native platforms, useBlocker uses React Navigation’s beforeRemove event. This blocks:
Note: Native blocking cannot prevent the app from being closed or backgrounded.
Edit this page on GitHub.