Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-beforeunload

jacobbuck255.8kMIT2.6.0TypeScript support: definitely-typed

React component and hook which listens to the beforeunload window event.

beforeunload, component, event, hook, onbeforeunload, react, unload, window

readme

react-beforeunload

Listen to the beforeunload window event in React.

Usage

useBeforeunload(handler);

Parameters

  • handler optional function to be called with BeforeUnloadEvent when beforeunload event is fired.

Example

import { useBeforeunload } from 'react-beforeunload';

const Example = (props) => {
  const [value, setValue] = useState('');

  useBeforeunload(value !== '' ? (event) => event.preventDefault() : null);

  return (
    <input onChange={(event) => setValue(event.target.value)} value={value} />
  );
};

Beforeunload Component

<Beforeunload onBeforeunload={handler} />

Props

  • onBeforeunload function to be called with BeforeUnloadEvent when beforeunload event is fired.

Example

import { Beforeunload } from 'react-beforeunload';

class Example extends React.Component {
  state = { value: '' };

  render() {
    return (
      <>
        {this.state.value !== '' && (
          <Beforeunload onBeforeunload={(event) => event.preventDefault()} />
        )}
        <input
          onChange={(event) => this.setState({ value: event.target.value })}
          value={this.state.value}
        />
      </>
    );
  }
}

:information_source: The Beforeunload component will render any children passed as-is, so it can be used as a wrapper component:

<Beforeunload onBeforeunload={…}>
  <MyApp />
</Beforeunload>

Custom message support

:warning: Some browsers used to display the returned string in the confirmation dialog, enabling the event handler to display a custom message to the user. However, this is deprecated and no longer supported in most browsers.

Source

To display a custom message in the triggered dialog box, return a string in the passed event handler function.

With useBeforeunload hook:

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

With Beforeunload component:

<Beforeunload onBeforeunload={() => 'You’ll lose your data!'} />

Requirements

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

changelog

Changelog

Unreleased

Added

  • Added conditional listening in useBeforeunload hook. (Fixes #9)
  • Added sideEffects property in package.json.

Changed

  • Updated handler parameter in useBeforeunload hook to be optional.

Removed

v2.5.3 - 2022-04-12

Changed

v2.5.2 - 2021-10-03

Fixed

  • Fixed legacy dialog activation using return "string"; method. (Fixes #27)

v2.5.1 - 2021-05-02

Removed

v2.5.0 - 2021-04-25

Added

Changed

  • Changed type checking in useBeforeunload hook to use invariant function.
  • Updated Beforeunload.propTypes to only be defined in non-production environments.
  • Updated internal event handler to set event.returnValue less times.

v2.4.0 - 2020-11-08

Added

  • Added source maps to build output.

Changed

  • Updated use-latest dependency to v1.2.0.
  • Updated react peer-dependency to support v17.

v2.3.0 - 2020-10-26

Changed

  • Improved type-checking.
  • Updated handler parameter of useBeforeunload hook to allow nullish values.

Removed

  • Removed defaultProps in favour of default values in object destructuring.

v2.2.4 - 2020-09-02

Changed

  • Updated Beforeunload.propTypes to only be defined in non-production environments.

v2.2.3 - 2020-08-28

Removed

  • Removed redundant type-check.

v2.2.2 - 2020-07-09

Changed

  • Used use-latest for handling refs in useBeforeunload hook.

v2.2.1 - 2020-05-20

Changed

  • Enabled loose mode on '@babel/preset-env' to reduce build output.

v2.2.0 - 2020-04-27

Added

  • Added ES Module build.
  • Added defaultProps to Beforeunload component.

v2.1.0 - 2019-06-23

Added

  • Added type-checking to useBeforeunload hook.

v2.0.1 - 2019-06-20

Added

  • Added Event.preventDefault() workaround for Chromium browsers.

Removed

  • Removed default export.

v2.0.0 - 2019-06-02

Added

  • Added useBeforeunload hook.

Changed

  • BREAKING Requires react peer-dependency to be v16.8.0 or newer.
  • BREAKING Beforeunload is now a named export.
  • Changed Beforeunload component to be functional and use hooks internally.

v1.1.1 - 2019-06-02

Fixed

  • Fixed failing builds due to missing Babel plugin.

v1.1.0 - 2019-06-02

Changed

  • Builds are now done with Rollup.

v1.0.4 - 2017-08-21

Changed

  • Updated react peer-dependency to support React 16.

v1.0.3 - 2017-07-28

Fixed

  • Fixed rendering when no children are set.

v1.0.2 - 2017-07-27

Fixed

  • Fixed publishing to NPM registry.

v1.0.1 - 2017-07-27

Fixed

  • Fixed wrong class being exported.

v1.0.0 - 2017-07-15

Initial public version! :tada: