You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Great package. I found this after I made my own. Just sharing this alternative. This alternative has typescript support and also supports complex data structures like arrays and nested objects. Relies on nextjs but can easily be replaced. Hope this helps someone!
import{useRouter}from"next/router";importqs,{IParseOptions,IStringifyOptions}from"qs";typeKeys<T>=Extract<keyofT,string>;typeO={[key:string]: any};/** * Variant to the original use url store that has an easier setup and allows for more complex datastructures * * Uses https://www.npmjs.com/package/qs * * NB: the base type needs to be an object! */exportconstmakeComplexUrlStore=<TextendsO>(): (<KextendsKeys<T>>(queryKey: K,)=>[T[K],(newValue: T[K]|undefined)=>Promise<boolean>,boolean])=>{consthookFactory=<KextendsKeys<T>>(queryKey: K,): [T[K],(newValue: T[K]|undefined)=>Promise<boolean>,boolean]=>{constrouter=useRouter();constqueryString=router.asPath.split("?")[1];/** * NB: These options have quite some overlap but may not be 100%. Careful */constqsParseOptions: IParseOptions&IStringifyOptions={allowDots: true,};constparsedQuery=qs.parse(queryString,qsParseOptions)asT;constvalue=parsedQuery[queryKey];constsetter=async(newValue: T[K]|undefined)=>{constnewState={ ...parsedQuery,[queryKey]: newValue};constnewQueryString=qs.stringify(newState,qsParseOptions);constpushed=awaitrouter.push(`${router.pathname}?${newQueryString}`,undefined,{shallow: true},);returnpushed;};return[value,setter,router.isReady];};returnhookFactory;};
The text was updated successfully, but these errors were encountered:
Great package. I found this after I made my own. Just sharing this alternative. This alternative has typescript support and also supports complex data structures like arrays and nested objects. Relies on nextjs but can easily be replaced. Hope this helps someone!
Usage:
And in your component:
Code
The text was updated successfully, but these errors were encountered: