MCPcopy Index your code
hub / github.com/amannn/next-query-params

github.com/amannn/next-query-params @v5.1.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v5.1.0 ↗ · + Follow
31 symbols 67 edges 30 files 0 documented · 0% 2 cross-repo links
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

next-query-params

[!WARNING]
This project is in maintainence mode now. I've originally created it for the Pages Router, but with the introduction of the App Router there are now better patterns for handling query params in my opinion. Personally, I parse searchParams in pages with zod. If you need something more advanced, you might want to have a look at nuqs.

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 (


My name is <input value={name} onChange={onNameInputChange} />


  );
}

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.

Core symbols most depended-on inside this repo

Shape

Function 31

Languages

TypeScript100%

Modules by API surface

packages/next-query-params/src/NextAdapterApp.tsx4 symbols
packages/example-pages/src/pages/index.tsx4 symbols
packages/example-app/src/app/scrollable/page.tsx4 symbols
packages/example-app/src/app/page.tsx4 symbols
packages/example-pages/src/pages/shallow-false.tsx3 symbols
packages/next-query-params/src/NextAdapterPages.tsx2 symbols
packages/example-pages/src/pages/shallow-true.tsx2 symbols
packages/example-pages/src/components/ShallowTest.tsx2 symbols
packages/example-pages/cypress/integration/index.spec.tsx2 symbols
packages/example-app/cypress/integration/index.spec.tsx2 symbols
packages/example-pages/src/pages/_app.tsx1 symbols
packages/example-app/src/app/layout.tsx1 symbols

Used by 2 indexed graphs manifest dependencies, hub-wide

For agents

$ claude mcp add next-query-params \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page