Where am I wrong?
useEffect(() => {
if (user) {
navigate(
{
pathname: `/dashboard`,
}
);
}
}, []);
const navigate = useNavigate();
const queries = new URLSearchParams(navigate.location.search);
After fetching query I perform queries.get which is again undefined, why?
The issue here is that it seems like you are assuming the
react-router-dom@6navigatefunction is the same thing as thereact-router-dom@5historyobject.navigate.locationis undefined, so an error is thrown when attempting to access asearchproperty.You don't need to read the
searchstring from alocationobject though becausereact-router-dom@6ships auseSearchParamshook to return the current URL's queryStringURLSearchParamsobject and a special navigation function.useSearchParams
The
useSearchParamshook is used to read and modify the query string in the URL for the current location. Like React's own useState hook,useSearchParamsreturns an array of two values: the current location's search params and a function that may be used to update them.Your code converted to get "queries" from `useSearchParams:
Get queryString parameters as per usual: