ffqs / react-beforeunload

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

react-beforeunload

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

Usage

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.

Source

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 />
</Beforeunload>

Requirements

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

About

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

License:MIT License


Languages

Language:JavaScript 100.0%