mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
More DRY code. Also move non-hooks to separate directories. BREAKING CHANGE: all `create*` factories been moved to `factory` subdirectory and in case direct import should be imported like `react-use/esm/factory/createBreakpoint` BREAKING CHANGE: `comps` directory renamed to `component`
34 lines
953 B
TypeScript
34 lines
953 B
TypeScript
import { useEffect, useState } from 'react';
|
|
import { isBrowser, off, on } from './misc/util';
|
|
|
|
const getValue = (search: string, param: string) => new URLSearchParams(search).get(param);
|
|
|
|
export type UseQueryParam = (param: string) => string | null;
|
|
|
|
const useSearchParam: UseQueryParam = (param) => {
|
|
const location = window.location;
|
|
const [value, setValue] = useState<string | null>(() => getValue(location.search, param));
|
|
|
|
useEffect(() => {
|
|
const onChange = () => {
|
|
setValue(getValue(location.search, param));
|
|
};
|
|
|
|
on(window, 'popstate', onChange);
|
|
on(window, 'pushstate', onChange);
|
|
on(window, 'replacestate', onChange);
|
|
|
|
return () => {
|
|
off(window, 'popstate', onChange);
|
|
off(window, 'pushstate', onChange);
|
|
off(window, 'replacestate', onChange);
|
|
};
|
|
}, []);
|
|
|
|
return value;
|
|
};
|
|
|
|
const useSearchParamServer = () => null;
|
|
|
|
export default isBrowser ? useSearchParam : useSearchParamServer;
|