🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

detect-browser-back-navigation

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

detect-browser-back-navigation

Detects when user clicks on the back button.

1.0.2
latest
Source
npm
Version published
Maintainers
1
Created
Source

detect-browser-back-navigation

Detects when user clicks on the back button.

The main use case is to creating modals, so you can close a modal when the user clicks on the phone back button, like in native mobile apps.

demo.gif

npm npm bundle size Module system npm type definitions

Installation

npm install detect-browser-back-navigation --save
yarn add detect-browser-back-navigation

Usage

Just call the default exported function with a callback on the first argument and the callback will fired whe the user clicks on the back button.

import detectBackButton from 'detect-browser-back-navigation';

const unsub = detectBackButton(() => {
    console.log('BACK BUTTON PRESSED!');
});

Note: You must properly unsubscribe when the modal is closed, to remove event listeners and avoid fire the callback multiple times leading in bugs.

See below an example of how to create a modal in React:

import detectBackButton from 'detect-browser-back-navigation';

export default function Modal(props) {
    const [show, setShow] = useState(false);

    useEffect(() => {
        if (show) {
            const unsub = detectBackButton(() => close());
            return unsub;
        }
    }, [show]);

    function open() {
        setShow(true);
    }
    function close() {
        setShow(false);
    }

    if (!show) return null;

    return (...);
}

Tip: This package also handles stacked back button calls, so you can display multiple modals and the package will callback correctly.

License

MIT

FAQs

Package last updated on 24 May 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts