mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
799 B
TypeScript
29 lines
799 B
TypeScript
import { getSearchParam } from '@/utils/location-helper';
|
|
import { useEffect, useState } from 'react';
|
|
|
|
export type UseQueryParam = (param: string) => string | null;
|
|
|
|
export const useSearchParam: UseQueryParam = (param) => {
|
|
const [value, setValue] = useState<string | null>(() =>
|
|
getSearchParam(param)
|
|
);
|
|
|
|
useEffect(() => {
|
|
const onChange = () => {
|
|
setValue(getSearchParam(param));
|
|
};
|
|
|
|
window.addEventListener('popstate', onChange);
|
|
window.addEventListener('pushstate', onChange);
|
|
window.addEventListener('replacestate', onChange);
|
|
|
|
return () => {
|
|
window.removeEventListener('popstate', onChange);
|
|
window.removeEventListener('pushstate', onChange);
|
|
window.removeEventListener('replacestate', onChange);
|
|
};
|
|
}, []);
|
|
|
|
return value;
|
|
};
|