
Security News
AI Slop Is Polluting Bug Bounty Platforms with Fake Vulnerability Reports
AI-generated slop reports are making bug bounty triage harder, wasting maintainer time, and straining trust in vulnerability disclosure programs.
@kimaramyz/use-query-params
Advanced tools
React hooks for using URL query params as state. No dependencies.
@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 encode and decode data of primitive type. 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 of URL query hooks
Provides useful options
No adapter required
Typescript support
Light-weight (5KB)
No dependencies.
ReactRouter
declare function useQueryParams<KeyEnum extends string>(options?: {
isShallow?: boolean;
}): [
{ [key in KeyEnum]?: string | undefined },
(queryParams: { [key in KeyEnum]?: unknown }) => void
];
import { FC } from 'react';
import { useQueryParams } from '@kimaramyz/use-query-params';
const BasicExample: FC = () => {
const [queryParams, setQueryParams] = useQueryParams<'page' | 'q'>();
return (
<div>
<h1>queryParams: {JSON.stringify(queryParams, null, 2)}</h1>
<button onClick={() => setQueryParams({ ...queryParams, page: 2 })}>
Upsert pageParam
</button>
<button
onClick={() => setQueryParams({ ...queryParams, page: undefined })}
>
Delete pageParam
</button>
<button onClick={() => setQueryParams(null)}>Clear</button>
</div>
);
};
import { FC } from 'react';
import { useQueryParams } from '@kimaramyz/use-query-params';
const ShallowRoutingExample: FC = () => {
const [queryParams, setQueryParams] = useQueryParams<'page' | 'q'>({
isShallow: true,
});
return (
<div>
<h1>queryParams: {JSON.stringify(queryParams, null, 2)}</h1>
<h2>history.length: {window.history.length}</h2>
<button onClick={() => setQueryParams({ ...queryParams, page: 2 })}>
Upsert pageParam
</button>
<button
onClick={() => setQueryParams({ ...queryParams, page: undefined })}
>
Delete pageParam
</button>
<button onClick={() => setQueryParams(null)}>Clear</button>
</div>
);
};
declare function useQueryParam<T = string>(
key: string,
options?: {
isShallow?: boolean;
}
): [T | null | undefined, (value: unknown) => void];
import { FC } from 'react';
import { useQueryParam } from '@kimaramyz/use-query-params';
const BasicExample: FC = () => {
const [page, setPage] = useQueryParam('page');
return (
<div>
<h1>page: {page}</h1>
<button onClick={() => setPage(1)}>Upsert</button>
<button onClick={() => setPage(undefined)}>Clear</button>
</div>
};
import { FC } from 'react';
import { useQueryParam } from '@kimaramyz/use-query-params';
const ShallowRoutingExample: FC = () => {
const [page, setPage] = useQueryParam('page', { isShallow: true });
return (
<div>
<h1>page: {page}</h1>
<h2>history.length: {window.history.length}</h2>
<button onClick={() => setPage(1)}>Upsert</button>
<button onClick={() => setPage(undefined)}>Clear</button>
</div>
);
};
declare function useQueryString(options?: {
isShallow?: boolean;
}): [
string,
(queryString: string | null | undefined, historyState?: unknown) => void
];
import { FC } from 'react';
import { useQueryString } from '@kimaramyz/use-query-params';
const BasicExample: FC = () => {
const [queryString, setQueryString] = useQueryString();
return (
<div>
<h1>queryString: {queryString}</h1>
<button onClick={() => setQueryString('?page=1&q=foo')}>Upsert</button>
<button onClick={() => setQueryString(undefined)}>Clear</button>
</div>
);
};
import { FC } from 'react';
import { useQueryString } from '@kimaramyz/use-query-params';
const ShallowRoutingExample: FC = () => {
const [queryString, setQueryString] = useQueryString({ isShallow: true });
return (
<div>
<h1>queryString: {queryString}</h1>
<h2>history.length: {window.history.length}</h2>
<button onClick={() => setQueryString('?page=1&q=foo')}>Upsert</button>
<button onClick={() => setQueryString(undefined)}>Clear</button>
</div>
);
};
FAQs
React hooks for using URL query params as state. No dependencies.
The npm package @kimaramyz/use-query-params receives a total of 8 weekly downloads. As such, @kimaramyz/use-query-params popularity was classified as not popular.
We found that @kimaramyz/use-query-params demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
AI-generated slop reports are making bug bounty triage harder, wasting maintainer time, and straining trust in vulnerability disclosure programs.
Research
Security News
The Socket Research team investigates a malicious Python package disguised as a Discord error logger that executes remote commands and exfiltrates data via a covert C2 channel.
Research
Socket uncovered npm malware campaign mimicking popular Node.js libraries and packages from other ecosystems; packages steal data and execute remote code.