Skip to content
🚪 React component and hook which listens to `beforeunload` on the window when mounted.
Branch: master
Clone or download
dependabot and jacobbuck Bump lodash from 4.17.11 to 4.17.15 (#11)
Bumps [lodash]( from 4.17.11 to 4.17.15.
- [Release notes](
- [Commits](lodash/lodash@4.17.11...4.17.15)

Signed-off-by: dependabot[bot] <>
Latest commit 7988429 Aug 31, 2019


React component and hook which listens to beforeunload on the window when mounted.


useBeforeunload Hook (recommended)

import { useBeforeunload } from 'react-beforeunload';

Display a dialog box:

useBeforeunload(event => event.preventDefault());

Display a dialog box with custom message:

useBeforeunload(() => "You'll lose your data!");

Some browsers display the returned string in the dialog box, others display a fixed message.


Beforeunload Component

import { Beforeunload } from 'react-beforeunload';

And use as you would use the hook:

<Beforeunload onBeforeunload={event => event.preventDefault()} />
<Beforeunload onBeforeunload={() => "You'll lose your data!"} />

Alternatively use it as a wrapper:

<Beforeunload onBeforeunload={}>
  <MyApp />


Requires a minimum of React version 16.8.0. If you're on an older version of React, then checkout v1.

You can’t perform that action at this time.