# react-hook-use-prevent-leave

> A react custom hook to prevent leaving by tab close or page refresh.

Latest version **1.1.1** (published 2022-07-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-hook-use-prevent-leave
pnpm add react-hook-use-prevent-leave
yarn add react-hook-use-prevent-leave
bun add react-hook-use-prevent-leave
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2022-07-23 |
| First published | 2022-07-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 15.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Charlie |
| Maintainers | tzyn.wang |
| Keywords | react, custom hook, prevent leave, block leave |

## Links

- npm: https://www.npmjs.com/package/react-hook-use-prevent-leave
- Repository: https://github.com/tzynwang/react-hook-use-prevent-leave
- Homepage: https://github.com/tzynwang/react-hook-use-prevent-leave#readme
- Issues: https://github.com/tzynwang/react-hook-use-prevent-leave/issues
- npm.io page: https://npm.io/package/react-hook-use-prevent-leave

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.1.1 (latest) — 2022-07-23
- 1.1.0 — 2022-07-22
- 1.0.0 — 2022-07-21
- 0.0.1-beta.4 — 2022-07-20
- 0.0.1-beta.3 — 2022-07-19
- 0.0.1-beta.2 — 2022-07-19
- 0.0.1-beta.1 — 2022-07-19

## README

# react hook: usePreventLeave

```string
npm i react-hook-use-prevent-leave
```

- A react custom hook to prompt confirm dialog when user tries to close the tab or refresh the page
- Supports TypeScript
- DEMO: https://tzynwang.github.io/react-hook-use-prevent-leave/
- NOTE: This hook will NOT block the navigation leave; to prompt a confirmation for this, use `Prompt` from `react-router-dom` (also includes in the example codes)

## Example

```tsx
import React, { useState } from 'react';
import { Prompt } from 'react-router-dom';
import usePreventLeave from 'react-hook-use-prevent-leave';

function PageShouldBeBlocked(): React.ReactElement {
  /* State */
  const [blockPage, setBlockPage] = useState<boolean>(true);

  /* Function */
  function togglePageBlock(): void {
    setBlockPage((prev) => !prev);
  }

  /* Hook */
  usePreventLeave(blockPage);
  // Or just call usePreventLeave() without passing any params, this mean the page will always be blocked.

  /* Main */
  return (
    <React.Fragment>
      <Prompt when={blockPage} message={'Confirm to leave this page?'} />
      <div>This page is {!blockPage && 'NOT'} block now.</div>
      <button onClick={togglePageBlock}>Click to toggle block or not.</button>
    </React.Fragment>
  );
}

export default PageShouldBeBlocked;

```

## Author

Charlie (Tzu Yin)\
[Blog](https://tzynwang.github.io/) | [GitHub](https://github.com/tzynwang)

---
_Source: https://npm.io/package/react-hook-use-prevent-leave · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
