npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-history-back-trap

v1.0.9

Published

Intercepts browser's back navigation allowing to resume it later if needed

Downloads

21

Readme

use-history-back-trap

by Vytenis Urbonavičius

use-history-back-trap is a React hook which can intercept native browser's navigation backwards in history. After navigation is intercepted and paused, it may later be resumed if needed.

By default browser navigation cannot be intercepted. use-history-back-trap uses a work-around to achieve interception by manipulating history object.


Use Case

Main use case is to prevent data loss if user is filling some form and then clicks "back" on browser's UI.

This should be obvious to everyone BUT I will highlight this just in case - do not use and abuse this tool for any malicious purposes such as preventing navigation on scam websites.


Limitations

Interception may not work if user has not interacted with page in any way (clicked or typed something).

If user attempts to navigate back for the second time in quick succession - second action might not be intercepted. This depends on whether "handleTrap" callback (see example below) finishes work soon enough to get ready for second interception.


Installation

npm install use-history-back-trap

Usage

import {useHistoryBackTrap} from 'use-history-back-trap'

const SomeFunctionalReactComponent = () => {
  useHistoryBackTrap(handleTrap)
  // < ... >
}

You need to provide implementation of handleTrap based on what you want to achieve.

Example of callback implementation:

// Note that this callback can be used async if needed
const handleTrap = async resume => {
  if (window.confirm('Are you sure?')) {
    resume() // triggers navigation which was suspended
    return true // tells that trap does NOT need to be setup again
  } else {
    return false // tells that trap needs to be setup again
  }
}

Additional options

useHistoryBackTrap accepts a second argument with additional options. All of them are optional. They are mainly designed to prevent clashes with other potential hacks in large projects - in most cases these options are not needed.

useHistoryBackTrap(handleTrap, {
  // Below options specify keys added into window.history.state.
  // They are used to identify navigation trap when history pop is detected.
  // If you do not know what that means - you do not need to change :)
  trapFlag: 'backTrap',
  trapTime: 'backTime',
})

Happy Hacking!!!