Skip to content

socheatsok78/use-refresh-lock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

use-refresh-lock

Ensure that user allow the browser to refresh

npm-version npm-minzip npm-download

Installation

yarn add use-refresh-lock

Usage

Create a simple refresh lock

import { useRefreshLock } from 'use-refresh-lock'

// create a lock
const release = useRefreshLock()

// now when you refresh the browser, the browse will prompt an
// 'Change you made may not be save.' prompt (aka Chrome)

// release the lock, just call the `release` method returned by `useRefreshLock`
release()

Aditionally, you could provide a callback to useRefreshLock. Recommended to use only to call navigator.sendBeacon only.

See: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon

import { useRefreshLock } from 'use-refresh-lock'

const release = useRefreshLock(function() {
    // execute before the page successfully unloaded/reloaded
    navigator.sendBeacon("/log", analyticsData);
})

License

License under MIT

About

useRefreshLock - Ensure that user allow the browser to refresh

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published