@ib-hooks/use-prevent-leave
Version:
React Hook to prompt the user for confirmation before leaving the page.
39 lines (25 loc) • 1.09 kB
Markdown
React Hook to **prompt the user for confirmation before leaving the page.**
`yarn add @ib-hooks/use-prevent-leave`
`npm i @ib-hooks/use-prevent-leave`
```js
import React from "react";
import usePreventLeave from "@ib-hooks/use-prevent-leave";
function App() {
const { enablePrevent, disablePrevent } = usePreventLeave();
const saveChanges = async () => {
enablePrevent();
await sendToApi();
disablePrevent();
};
return <button onClick={saveChanges}>Save Changes</button>;
}
```
| **Return value** | **Type** | **Description** |
| ---------------- | -------- | --------------------------------------------------------------------------------------------------------------------------------- |
| Functions | object | An object containing functions `enablePrevent` and `disablePrevent`, use this functions to enable/disable the leaving prevention. |