Registers a beforeunload event listener that invokes the given callback.
beforeunload
The callback can invoke event.preventDefault() and set event.returnValue to a string
event.preventDefault()
event.returnValue
callback handler
whether to register the event listener
Prompt the user before leaving the page.
import { useBeforeUnload } from "@trashpanda001/helpers/react"function Component() { useBeforeUnload((event) => { event.preventDefault() event.returnValue = "Are you sure you want to leave?" }) return <div>Page</div>} Copy
import { useBeforeUnload } from "@trashpanda001/helpers/react"function Component() { useBeforeUnload((event) => { event.preventDefault() event.returnValue = "Are you sure you want to leave?" }) return <div>Page</div>}
Registers a
beforeunload
event listener that invokes the given callback.The callback can invoke
event.preventDefault()
and setevent.returnValue
to a string