Skip to content

Commit

Permalink
Add useBeforeUnload react hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyalesik committed Apr 10, 2019
1 parent 49a86a8 commit a428a15
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
- [`useAsync`](./docs/useAsync.md) — resolves an `async` function.
- [`useAsyncFn`](./docs/useAsyncFn.md) — state management for an async function
- [`useAsyncRetry`](./docs/useAsyncRetry.md) — `useAsync` with `retry()` method.
- [`useBeforeUnload`](./docs/useBeforeUnload.md) — shows browser alert when user try to reload or close the page.
- [`useCopyToClipboard`](./docs/useCopyToClipboard.md) — copies text to clipboard.
- [`useDebounce`](./docs/useDebounce.md) — debounces a function. [![][img-demo]](https://streamich.github.io/react-use/?path=/story/side-effects-usedebounce--demo)
- [`useFavicon`](./docs/useFavicon.md) — sets favicon of the page.
Expand Down
16 changes: 16 additions & 0 deletions docs/useBeforeUnload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# `useBeforeUnload`

React side-effect hook that shows browser alert when user try to reload or close the page.


## Usage

```jsx
import {useBeforeUnload} from 'react-use';

const Demo = () => {
useBeforeUnload('Are you sure?');

return null;
};
```
18 changes: 18 additions & 0 deletions src/__stories__/useBeforeUnload.story.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {storiesOf} from '@storybook/react';
import * as React from 'react';
import {useBeforeUnload} from '..';
import ShowDocs from './util/ShowDocs';

const Demo = () => {
useBeforeUnload('Are you sure?');

return (
<div>
Try to reload or close tab
</div>
);
};

storiesOf('Side effects|useBeforeUnload', module)
.add('Docs', () => <ShowDocs md={require('../../docs/useBeforeUnload.md')} />)
.add('Demo', () => <Demo/>)
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import useAsyncFn from './useAsyncFn';
import useAsyncRetry from './useAsyncRetry';
import useAudio from './useAudio';
import useBattery from './useBattery';
import useBeforeUnload from './useBeforeUnload'
import useBoolean from './useBoolean';
import useCopyToClipboard from './useCopyToClipboard';
import useDrop from './useDrop';
Expand Down Expand Up @@ -76,6 +77,7 @@ export {
useAsyncRetry,
useAudio,
useBattery,
useBeforeUnload,
useBoolean,
useCopyToClipboard,
useDrop,
Expand Down
17 changes: 17 additions & 0 deletions src/useBeforeUnload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {useEffect} from "react";

const useBeforeUnload = (message?: string) => {
useEffect(() => {
window.onbeforeunload = e => {
e.returnValue = message;
return message;
};

return () => {
window.onbeforeunload = null;
return;
}
}, [message]);
};

export default useBeforeUnload;

0 comments on commit a428a15

Please sign in to comment.