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@6
navigate
function is the same thing as thereact-router-dom@5
history
object.navigate.location
is undefined, so an error is thrown when attempting to access asearch
property.You don't need to read the
search
string from alocation
object though becausereact-router-dom@6
ships auseSearchParams
hook to return the current URL's queryStringURLSearchParams
object and a special navigation function.useSearchParams
The
useSearchParams
hook is used to read and modify the query string in the URL for the current location. Like React's own useState hook,useSearchParams
returns 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: