usePageLeave

Track when a user navigates away from a webpage with usePageLeave.

Install:

Note: This hook depends on React’s experimental useEffectEvent.

npm i @uidotdev/usehooks@experimental react@experimental react-dom@experimental

Description:

The usePageLeave hook is useful for tracking when a user leaves a webpage or navigates away from it. When the mouseout event is triggered, the callback function is executed only if the user’s cursor moves outside the webpage or onto an element outside the webpage (e.g., the browser’s address bar). This hook is beneficial for scenarios where you need to perform specific actions when a user leaves your webpage, such as saving user progress, displaying a confirmation prompt, or triggering analytics events.

Parameters

NameTypeDescription
cbfunctionThe callback function to be invoked.

Demo:

Example:

More Hooks:

Sort: