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

next-query-params

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

next-query-params

Convenient state management of query parameters in Next.js apps.

  • 4.2.3-alpha.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
32K
decreased by-10.73%
Maintainers
1
Weekly downloads
 
Created
Source

next-query-params

Gzipped size Tree shaking supported

Convenient state management of query parameters in Next.js apps.

Persisting React state to query parameters is often a good idea:

  1. When the URL is shared, the app state is restored. Same applies to bookmarks.
  2. When using the browser back button, the state of the previous page is restored.
  3. When navigating forward to a page the user was already on, the state is reset.

This library is an adapter for use-query-params to integrate with Next.js.

Installation

npm install next-query-params use-query-params

App Router

// app/layout.tsx

'use client';

import NextAdapterApp from 'next-query-params/app';
import {QueryParamProvider} from 'use-query-params';

export default function RootLayout({children}) {
  return (
    <html lang="en">
      <body>
        <QueryParamProvider adapter={NextAdapterApp}>
          {children}
        </QueryParamProvider>
      </body>
    </html>
  );
}

Pages Router

// _app.tsx
import NextAdapterPages from 'next-query-params/pages';
import {QueryParamProvider} from 'use-query-params';

export default function App({Component, pageProps}) {
  return (
    <QueryParamProvider adapter={NextAdapterPages}>
      <Component {...pageProps} />
    </QueryParamProvider>
  );
}

Usage

Please refer to the usage of use-query-params.

import {useQueryParam, StringParam, withDefault} from 'use-query-params';

export default function IndexPage() {
  const [name, setName] = useQueryParam('name', withDefault(StringParam, ''));

  function onNameInputChange(event) {
    setName(event.target.value);
  }

  return (
    <p>My name is <input value={name} onChange={onNameInputChange} /></p>
  );
}

Shallow routing (Pages Router-only)

NextAdapter can be configured to opt-out of shallow routing. In this case server-side functions like getServerSideProps will be run again when a query parameter changes.

// _app.tsx
import NextAdapterPages from 'next-query-params/pages';
import {QueryParamProvider} from 'use-query-params';

function Adapter(props) {
  return <NextAdapter {...props} shallow={false} />;
}

export default function App({Component, pageProps}) {
  return (
    <QueryParamProvider adapter={Adapter}>
      <Component {...pageProps} />
    </QueryParamProvider>
  );
}

Credits

This library is an adapter for use-query-params by Peter Beshai and was originally based on the code that was collaboratively created in use-query-params#13.

Keywords

FAQs

Package last updated on 12 Jul 2023

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