Socket
Socket
Sign inDemoInstall

better-state

Package Overview
Dependencies
2
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    better-state

This is a small wrapper that encapsulates * [history.pushState()](https://developer.mozilla.org/en-US/docs/Web/API/History/pushState) * [history.replaceState()](https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState), and * [window.onpop


Version published
Maintainers
1
Created

Readme

Source

Better-State

Including "Not Going Back"

This is a small wrapper that encapsulates

  • history.pushState()

  • history.replaceState(), and

  • window.onpopstate()

Features

  1. View the browser history[^1] for your app.

  2. Prevent the use of the browser's back and forward buttons.

  3. Includes the current window.scroll for conveneince (useful when using History.scrollRestoration = 'manual')

Installation

npm install better-state

Setup

import betterState from "better-state";

Usage

To change the current browser URL and add to the browser history: betterState.pushState(url) betterState.pushState(url,{--some consumer-data})


To prevent the use of the browser's back and forward buttons: betterState.preventBack = true betterState.preventForward = true


To get the current known history[^1]: betterState.history


To subscribe to popstate events (i.e. back and forward button clicks...) betterState.on("state-change", (e) => { console.log(e); });

returns:

  • back: [ true | false ] if the back button was pressed.
  • forward: [ true | false ] if the back button was pressed.
  • prevented: [ true | false ] if the pop was prevented.
  • forced: [ true | false ] if the pop was forced[^2]
  • state: {Object} including:
    • url
    • scrollX
    • scrollY
    • data: {Object} any consumer data

To override preventBack and preventForward: betterState.forceForward() betterState.forceBack()


To replace the current state: (i.e. to change the current URL without adding to the browser history) betterState.replaceState(url) betterState.replaceState(url,{--some consumer-data})


To get the current state: betterState.state


[^1] Only includes better-state history. [^2] See forceBack() and forceForward().

FAQs

Last updated on 10 Apr 2022

Did you know?

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc