React hooks for using URL query params as state. No dependencies.
npm install @kimaramyz/use-query-params@kimaramyz/use-query-params is a library of React hooks for using URL query params as state. Light-weight, TS support and no dependencies. This allows you to easily synchronize(encode and decode) react state with URL query parameters. Implemented by History API and URLSearchParams API.
When creating apps with easily shareable URLs, you often want to encode state as query parameters, but all query parameters must be encoded as strings.
If you are doing a React-based project, you will probably be using ReactRouter or NextRouter together. However, the part where these routers use query string as a state, i.e., useSearchParams, returns instance of URLSearchParams, so you may need to parse it again. Therefore, this library can help you when you're trying to use query params with ReactRouter or NextRouter, and the other History API-based router libraries.
- Provides three options for using query params.
- useQueryParams
- useQueryParam
- useQueryString
- Provides useful options
- Shallow routing - Able to update query parameter with no history changes
- Hard/Soft key-value deletion (Upcoming)
- No adapter required
- Typescript support
- Light-weight (5KB)
- No dependencies.
- No serializer library dependent
- No router library dependent
- Perfectly compatible with any version of ReactRouter
Type declaration
``ts`
declare function useQueryParams
isShallow?: boolean;
}): [
{ [key in KeyEnum]?: string | undefined },
(queryParams: { [key in KeyEnum]?: unknown }) => void
];
#### Basic example
`tsx
import { FC } from 'react';
import { useQueryParams } from '@kimaramyz/use-query-params';
const BasicExample: FC = () => {
const [queryParams, setQueryParams] = useQueryParams<'page' | 'q'>();
return (
#### Using shallow routing
`tsx
import { FC } from 'react';
import { useQueryParams } from '@kimaramyz/use-query-params';const ShallowRoutingExample: FC = () => {
const [queryParams, setQueryParams] = useQueryParams<'page' | 'q'>({
isShallow: true,
});
return (
queryParams: {JSON.stringify(queryParams, null, 2)}
history.length: {window.history.length}
onClick={() => setQueryParams({ ...queryParams, page: undefined })}
>
Delete pageParam
);
};
`
$3
Type declaration
`ts
declare function useQueryParam(
key: string,
options?: {
isShallow?: boolean;
}
): [T | null | undefined, (value: unknown) => void];
`#### Basic example
`tsx
import { FC } from 'react';
import { useQueryParam } from '@kimaramyz/use-query-params';const BasicExample: FC = () => {
const [page, setPage] = useQueryParam('page');
return (
page: {page}
};
`#### Using shallow routing
`tsx
import { FC } from 'react';
import { useQueryParam } from '@kimaramyz/use-query-params';const ShallowRoutingExample: FC = () => {
const [page, setPage] = useQueryParam('page', { isShallow: true });
return (
page: {page}
history.length: {window.history.length}
);
};
`
$3
Type declaration
`ts
declare function useQueryString(options?: {
isShallow?: boolean;
}): [
string,
(queryString: string | null | undefined, historyState?: unknown) => void
];
`#### Basic example
`tsx
import { FC } from 'react';
import { useQueryString } from '@kimaramyz/use-query-params';const BasicExample: FC = () => {
const [queryString, setQueryString] = useQueryString();
return (
queryString: {queryString}
);
};
`#### Using shallow routing
`tsx
import { FC } from 'react';
import { useQueryString } from '@kimaramyz/use-query-params';const ShallowRoutingExample: FC = () => {
const [queryString, setQueryString] = useQueryString({ isShallow: true });
return (
queryString: {queryString}
history.length: {window.history.length}
);
};
``