Created
July 10, 2021 21:49
-
-
Save wscnd/3bb5e6805c6f567d8c62c50fc8ac97f6 to your computer and use it in GitHub Desktop.
Revisions
-
wscnd created this gist
Jul 10, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ const getSearchParams = <T extends object>(): Partial<T> => { // server side rendering if (typeof window === "undefined") { return {}; } const params = new URLSearchParams(window.location.search); return new Proxy(params, { get(target, prop, receiver) { return target.get(prop as string) || undefined; }, }) as T; }; export const useSearchParams = <T extends object = any>(): Partial<T> => { const [searchParams, setSearchParams] = useState(getSearchParams()); useEffect(() => { setSearchParams(getSearchParams()); }, [typeof window === "undefined" ? "once" : window.location.search]); return searchParams; };