Gathering detailed insights and metrics for @kimaramyz/use-query-params
Gathering detailed insights and metrics for @kimaramyz/use-query-params
Gathering detailed insights and metrics for @kimaramyz/use-query-params
Gathering detailed insights and metrics for @kimaramyz/use-query-params
Powerful state management for URL query parameter. Light-weight, TS support and no dependencies.
npm install @kimaramyz/use-query-params
Typescript
Module System
Min. Node Version
61.8
Supply Chain
92
Quality
75.5
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Love this project? Help keep it running — sponsor us today! 🚀
Total Downloads
832
Last Day
3
Last Week
6
Last Month
24
Last Year
194
4 Stars
33 Commits
1 Watching
1 Branches
1 Contributors
Minified
Minified + Gzipped
Latest Version
0.3.3
Package Id
@kimaramyz/use-query-params@0.3.3
Unpacked Size
11.46 kB
Size
3.81 kB
File Count
14
Cumulative downloads
Total Downloads
Last day
0%
3
Compared to previous day
Last week
0%
6
Compared to previous week
Last month
-22.6%
24
Compared to previous month
Last year
-19.2%
194
Compared to previous year
@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.
Provides useful options
No adapter required
Typescript support
Light-weight (5KB)
No dependencies.
ReactRouter
1declare function useQueryParams<KeyEnum extends string>(options?: { 2 isShallow?: boolean; 3}): [ 4 { [key in KeyEnum]?: string | undefined }, 5 (queryParams: { [key in KeyEnum]?: unknown }) => void 6];
1import { FC } from 'react'; 2import { useQueryParams } from '@kimaramyz/use-query-params'; 3 4const BasicExample: FC = () => { 5 const [queryParams, setQueryParams] = useQueryParams<'page' | 'q'>(); 6 7 return ( 8 <div> 9 <h1>queryParams: {JSON.stringify(queryParams, null, 2)}</h1> 10 <button onClick={() => setQueryParams({ ...queryParams, page: 2 })}> 11 Upsert pageParam 12 </button> 13 <button 14 onClick={() => setQueryParams({ ...queryParams, page: undefined })} 15 > 16 Delete pageParam 17 </button> 18 <button onClick={() => setQueryParams(null)}>Clear</button> 19 </div> 20 ); 21};
1import { FC } from 'react'; 2import { useQueryParams } from '@kimaramyz/use-query-params'; 3 4const ShallowRoutingExample: FC = () => { 5 const [queryParams, setQueryParams] = useQueryParams<'page' | 'q'>({ 6 isShallow: true, 7 }); 8 9 return ( 10 <div> 11 <h1>queryParams: {JSON.stringify(queryParams, null, 2)}</h1> 12 <h2>history.length: {window.history.length}</h2> 13 <button onClick={() => setQueryParams({ ...queryParams, page: 2 })}> 14 Upsert pageParam 15 </button> 16 <button 17 onClick={() => setQueryParams({ ...queryParams, page: undefined })} 18 > 19 Delete pageParam 20 </button> 21 <button onClick={() => setQueryParams(null)}>Clear</button> 22 </div> 23 ); 24};
1declare function useQueryParam<T = string>( 2 key: string, 3 options?: { 4 isShallow?: boolean; 5 } 6): [T | null | undefined, (value: unknown) => void];
1import { FC } from 'react'; 2import { useQueryParam } from '@kimaramyz/use-query-params'; 3 4const BasicExample: FC = () => { 5 const [page, setPage] = useQueryParam('page'); 6 7 return ( 8 <div> 9 <h1>page: {page}</h1> 10 <button onClick={() => setPage(1)}>Upsert</button> 11 <button onClick={() => setPage(undefined)}>Clear</button> 12 </div> 13};
1import { FC } from 'react'; 2import { useQueryParam } from '@kimaramyz/use-query-params'; 3 4const ShallowRoutingExample: FC = () => { 5 const [page, setPage] = useQueryParam('page', { isShallow: true }); 6 7 return ( 8 <div> 9 <h1>page: {page}</h1> 10 <h2>history.length: {window.history.length}</h2> 11 <button onClick={() => setPage(1)}>Upsert</button> 12 <button onClick={() => setPage(undefined)}>Clear</button> 13 </div> 14 ); 15};
1declare function useQueryString(options?: { 2 isShallow?: boolean; 3}): [ 4 string, 5 (queryString: string | null | undefined, historyState?: unknown) => void 6];
1import { FC } from 'react'; 2import { useQueryString } from '@kimaramyz/use-query-params'; 3 4const BasicExample: FC = () => { 5 const [queryString, setQueryString] = useQueryString(); 6 7 return ( 8 <div> 9 <h1>queryString: {queryString}</h1> 10 <button onClick={() => setQueryString('?page=1&q=foo')}>Upsert</button> 11 <button onClick={() => setQueryString(undefined)}>Clear</button> 12 </div> 13 ); 14};
1import { FC } from 'react'; 2import { useQueryString } from '@kimaramyz/use-query-params'; 3 4const ShallowRoutingExample: FC = () => { 5 const [queryString, setQueryString] = useQueryString({ isShallow: true }); 6 7 return ( 8 <div> 9 <h1>queryString: {queryString}</h1> 10 <h2>history.length: {window.history.length}</h2> 11 <button onClick={() => setQueryString('?page=1&q=foo')}>Upsert</button> 12 <button onClick={() => setQueryString(undefined)}>Clear</button> 13 </div> 14 ); 15};
No vulnerabilities found.
No security vulnerabilities found.