next-navigation-entries
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Next Navigation Entries

this is an experimental library for using browser history stack as a react state in Next.js app. the term navigation-entries is from experimental Navigation API.

this will work stable under specific conditions:

  1. your Next.js app uses pages router
  2. each element in the browser history stack is not duplicated
const stableEntries = ["/", "/products", "/products/123", "/cart"]; // ok
const unstableEntries = ["/", "/cart", "/products", "/products/123", "/cart"]; // not ok

considering its limitations, I would suggest using it for developing a WebView app.

Installation

yarn add next-navigation-entries

or

npm install next-navigation-entries

Usage

first, wrap your app with provider component.

// _app.js
import { NavigationEntriesProvider } from "next-navigation-entries";

const App = ({ Component, pageProps }) => {
  //...
  return (
    <NavigationEntriesProvider>
      <Component {...pageProps} />
    </NavigationEntriesProvider>
  );
};

then use the navigation entries as a state with useNavigationEntries() hook in component.

// BookingCompleteScreen.js
import { useNavigationEntries } from "next-navigation-entries";

const BookingCompleteScreen = () => {
  const entries = useNavigationEntries();

  const goBackToInitialPage = () => {
    const delta = 1 - entries.length;
    history.go(delta);
  };

  return <button onClick={goBackToInitialPage}>back to home</button>;
};

Package Sidebar

Install

npm i next-navigation-entries

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

21.2 kB

Total Files

9

Last publish

Collaborators

  • twp44k