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
we typically use useSearchParams in nextjs ( or other related hooks in react ), in order to get a query from the url, it's ok , but you need to be specific which query you are looking for and pass it as string to searchParams.get("name").
by using this function (getUrlParams) , we can get all queries in the url at a time. and destructor the one we need.
as an example :
imagine we have this url
sample.com/page?test=idk&orderBy=DATE
and need to get orderBy query, we simply do this :
const{orderBy}=getUrlParams();// it return all queries {test:"idk",orderBy:"DATE"}console.log(orderBy);// DATE
The text was updated successfully, but these errors were encountered:
Hey @gooddevil79, thanks for the proposal. I have a few comments on the current implementation:
We should avoid using window in our functions as it couples the implementation to the browser, which we aim to avoid in the toolbox. This makes it difficult to test and renders the function unusable in other runtimes like Deno or Node.js. I suggest we get the search params from the parameters.
There's a standard way of parsing search params using URLSearchParams, I strongly suggest using it, as all runtime supports and handles all cases (even for this implementation)
constgetUrlParams=(search)=>{constparams=newURLSearchParams(search);constparamsObj={};for(const[key,value]ofparams){paramsObj[key]=isNaN(value) ? value : parseFloat(value);}returnparamsObj;};
There are some uncovered cases, for instance, What happens if we have ?items[]=10&items[]=20? which is a valid query parameter and is usually used by some frameworks to represent arrays. this implementation overrides the latest one and ignores others.
Implicit type conversion can also lead to unexpected behavior. Consider this example:
Function Signature
Motivation
we typically use useSearchParams in nextjs ( or other related hooks in react ), in order to get a query from the url, it's ok , but you need to be specific which query you are looking for and pass it as string to
searchParams.get("name")
.by using this function (getUrlParams) , we can get all queries in the url at a time. and destructor the one we need.
as an example :
imagine we have this url
and need to get orderBy query, we simply do this :
The text was updated successfully, but these errors were encountered: