Router
useQueryParams
Hook for accessing and manipulating URL search parameters.
useQueryParams()
The useQueryParams hook provides access to URL search parameters with a standard URLSearchParams interface.
Hook Signature
import { } from 'manicjs';
export default function () {
const = ();
// ...
}Return Value
Returns a URLSearchParams object with standard methods:
class URLSearchParams {
get(name: string): string | null;
getAll(name: string): string[];
has(name: string): boolean;
set(name: string, value: string): void;
append(name: string, value: string): void;
delete(name: string): void;
forEach(callback: (value: string, key: string) => void): void;
toString(): string;
}Examples
Reading Query Params
import React from 'react';
import { } from 'manicjs';
declare const : (: string) => void;
export function () {
const = ();
const = .('q') || '';
const = .('page') || '1';
const = .('sort') || 'date';
return (
<>
<>Results for "{}"</>
<>Page {}, sorted by {}</>
</>
);
}Updating Query Params
import React from 'react';
import { } from 'manicjs';
declare const : (: string) => void;
export function () {
const = ();
const = (: string) => {
const = new ();
.('q', );
.('page', '1'); // Reset pagination
// Update URL without navigation
(`?${.()}`);
};
return (
<
={.('q') || ''}
={(: any) => (.target.value)}
/>
);
}Pagination
import React from 'react';
import { } from 'manicjs';
declare const : (: string) => void;
export function () {
const = ();
const = (.('page') || '1', 10);
const = (: number) => {
const = new ();
.('page', ());
(`?${.()}`);
};
return (
<>
< ={ <= 1} ={() => ( - 1)}>
Previous
</>
<>Page {}</>
< ={() => ( + 1)}>
Next
</>
</>
);
}Multiple Values
import React from 'react';
import { } from 'manicjs';
declare const : (: string) => void;
export function () {
const = ();
// getAll returns array for multiple values
const = .('tag');
// ['react', 'typescript']
const = (: string) => {
const = new ();
const = .('tag');
if (.()) {
// Remove tag
.('tag');
.( => !== ).( => .('tag', ));
} else {
// Add tag
.('tag', );
}
(`?${.()}`);
};
return (
<>
{.( => (
< ={}>{}</>
))}
</>
);
}With useEffect
import React, { , } from 'react';
import { } from 'manicjs';
declare const : (: string) => void;
export function () {
const = ();
const [, ] = <{ : string[] } | null>(null);
// Refetch when query params change
(() => {
const = .('q');
const = .('page');
(`/api/items?q=${}&page=${}`)
.( => .())
.((: any) => ());
}, [.()]); // Re-run when params string changes
if (!) return <>Loading...</>;
return <>{.}</>;
}Type Definitions
declare function useQueryParams(): URLSearchParams;URL Patterns
| URL | searchParams.get() Result |
|---|---|
/search?q=react | q: "react" |
/posts?sort=date&page=2 | sort: "date", page: "2" |
/filter?tag=a&tag=b | getAll('tag'): ["a", "b"] |
/empty | All methods return null/[] |