I have an app which sometimes is loaded with a query string param t.
At the start, I want the app to read this param if available and remove it from the URL.
On the root component, I am doing this:
const [searchParams, setSearchParams] = useSearchParams();
if (searchParams.has('t')) {
const token = searchParams.get('t');
if (token) {
searchParams.delete('t');
const newParams: {[key: string]: string} = {};
searchParams.forEach((value: string, key: string) => {
newParams[key] = value;
});
console.log('setting params: ');
console.dir(newParams);
setSearchParams(newParams);
AuthController.setAccessToken(token);
}
}
I see that it correctly reads param t and the newParams object is empty or contains only the other parameters, but for some reason setSearchParams(newParams) seems to not be doing anything. Parameter t is still in the URL.
How I can have it remove this parameter from the URL?
setSearchParams(newParams)all you are doing is updating state of the component. That won't touch the URL itself. I've done some thing similar w/v5 but since you are using v6 I prefer not to add it as an answer. But for v5, I useduseHistory& my code washistory.push({pathname: <base route>, search: stringify(currentParams),});And,const currentParams = parse(window.location.search.replace("?", ""));useNavigateI think, but haven't had the chance to work w/v6.