next-query-params
Convenient state management of query parameters in Next.js apps.
Persisting React state to query parameters is often a good idea:
- When the URL is shared, the app state is restored. Same applies to bookmarks.
- When using the browser back button, the state of the previous page is restored.
- When navigating forward to a page the user was already on, the state is reset.
Note that this library is a small wrapper for use-query-params
to integrate with Next.js.
Installation
npm install next-query-params
import {NextQueryParamProvider} from 'next-query-params';
export default function App({Component, pageProps}) {
return (
<NextQueryParamProvider>
<Component {...pageProps} />
</NextQueryParamProvider>
);
}
Usage
Please refer to the usage of use-query-params
. This library configures the provider for usage with Next.js and additionally re-exports all modules from use-query-params
for convenience.
Note that unlike use-query-params
, this library has all dependencies included and compiled to support IE11.
import {useQueryParam, StringParam, withDefault} from 'next-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>
);
}
export function getServerSideProps() {
return {props: {}};
}
Credits
This library is a small wrapper around use-query-params
by Peter Beshai and is based on the code that was collaboratively created in use-query-params#13.