• Stars
    star
    163
  • Rank 231,141 (Top 5 %)
  • Language
    TypeScript
  • License
    Other
  • Created over 3 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convenient state management of query parameters in Next.js apps

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.

Note that 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 is based on the code that was collaboratively created in use-query-params#13.

More Repositories

1

next-intl

๐ŸŒ Internationalization (i18n) for Next.js
TypeScript
2,442
star
2

action-semantic-pull-request

A GitHub Action that ensures that your PR title matches the Conventional Commits spec
JavaScript
964
star
3

next-client-script

Supercharge the performance of your Next.js apps by using a minimal client runtime ๐Ÿš€
TypeScript
77
star
4

react-keep-state

Make React components keep their state, even if they're unmounted.
JavaScript
30
star
5

dokku-node-hello-world

An instruction on how to deploy a node app to a server that is running dokku.
JavaScript
29
star
6

react-hooks

A collection of commonly used hooks for React apps
TypeScript
16
star
7

friends-ui

An animation experiment.
JavaScript
13
star
8

street-photography-viewer

TypeScript
12
star
9

atom-format-javascript-comment

Formats your JavaScript comments
JavaScript
8
star
10

gulp-wordpress-template

A very basic proof-of-concept, showing that gulp can work as a replacement for MAMP/XAMPP and also extend their features for SASS compiling for example.
PHP
7
star
11

multi-sitemap

Painless creation of large dynamic sitemaps that consist of multiple files
TypeScript
6
star
12

next-intl-example-rsc

TypeScript
6
star
13

amann.work

My personal website featuring some projects I've worked on.
JavaScript
4
star
14

graphql-filter-fragment

Filters a data structure by a GraphQL fragment
TypeScript
3
star
15

jest-globals-bug

JavaScript
3
star
16

animated-routes

JavaScript
2
star
17

semantic-release-test

Only for personal testing โ€ฆ
JavaScript
2
star
18

webpack-top-level-await

JavaScript
2
star
19

ea-ss24-sem4

TypeScript
2
star
20

next-dynamic-loading

CSS
1
star
21

eslint-plugin-react-test

TypeScript
1
star
22

nextjs-tsconfig-paths

JavaScript
1
star
23

nextjs-commerce-example

TypeScript
1
star
24

babel-preset-react-hot

Babel preset for React hot module replacement and error catching
JavaScript
1
star
25

nextjs-server-context

TypeScript
1
star
26

next-compile-bug

TypeScript
1
star
27

photo-gallery

A gesture-based photo gallery
SCSS
1
star
28

nextjs-bug-use-server

JavaScript
1
star
29

nextjs13-query-params

TypeScript
1
star
30

vite-plugin-pretty-css-modules

Use prettier generated class names in development, while applying minification in production.
TypeScript
1
star
31

nextjs-bugrepro-sitemap

TypeScript
1
star
32

next-intl-mixed-routing

TypeScript
1
star
33

nextjs-bug-repro-middleware-import

TypeScript
1
star
34

nodejs-esm-test

JavaScript
1
star