Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

next-use-contextual-routing

Package Overview
Dependencies
Maintainers
0
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-use-contextual-routing

Generate Next.js contextual routing paths

  • 4.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.9K
increased by1.18%
Maintainers
0
Weekly downloads
 
Created
Source

Next use contextual routing

Build status Test coverage report Npm version Bundle size

Intro

React hook that generates round trip hrefs to enable contextual routing for Next.js. Take a look to the demo 👈.

Contextual routing in action

import React from 'react';
import Link from 'next/link';
import { useContextualRouting } from 'next-use-contextual-routing';

function MyPage() {
  const { makeContextualHref, returnHref } = useContextualRouting();

  return (
    <ul>
      <li>
        <Link
          href={makeContextualHref({ id: 33 })}
          as="/route-to-visit-contextually"
          shallow
        >
          Start contextual routing
        </Link>
      </li>
      <li>
        <Link href={returnHref} shallow>
          Back to where contextual routing was started
        </Link>
      </li>
    </ul>
  );
}

With Next router

import React from 'react';
import { useRouter } from 'next/router';
import { useContextualRouting } from 'next-use-contextual-routing';

function MyPage() {
  const router = useRouter();
  const { makeContextualHref, returnHref } = useContextualRouting();

  const openModal = () =>
    router.push(
      makeContextualHref({ id: 33 }),
      '/route-to-visit-contextually',
      {
        shallow: true,
      }
    );

  const closeModal = () =>
    router.push(returnHref, undefined, { shallow: true });

  return (
    <ul>
      <li>
        <button onClick={openModal}>Open contextual modal</button>
      </li>
      <li>
        <button onClick={closeModal}>Close contextual modal</button>
      </li>
    </ul>
  );
}

Why?

Contextual routing presents 2 challenges:

  • Persist a href string able to keep the initial page state consistent throughout the whole contextual navigation
  • Persist the url to return to when contextual routing is terminated

Next use contextual routing abstracts these responsibilities as a React hook.

It also works in statically exported apps and supports navigation with browsers' back button.

API

const { makeContextualHref, returnHref } = useContextualRouting();
makeContextualHref
const makeContextualHref: (
  extraParams?: Record<string, string | number>
) => string;

The function returns the path to provide as href to start or keep alive contextual navigation. The generated path describes the state of the page to keep alive while contextual navigation is active.

It optionally accepts an object describing extra parameters to append to contextual navigation href.

returnHref
const returnHref: string;

The path to return to to close contextual navigation.

Next.js versions support

Next use contextual routingNext.js
v2.2.0v9.X -> v12.X
v3.0.0v13.X
v3.2.0 +v13.X -> v14.X
v4.0.0 +v15.X

Notes

Naming

Contextual routing pattern is also known as:

Scroll behaviour on refresh

When using Contextual routing, default browsers scroll behaviour on page refresh might produce undesired results. If the case, setting history.scrollRestoration to manual could improve user experience.

history.scrollRestoration = 'manual';

Devnote: deploy Next.js on GitHub pages

https://www.viget.com/articles/host-build-and-deploy-next-js-projects-on-github-pages

Keywords

FAQs

Package last updated on 27 Oct 2024

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

SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc