T
TanStack5mo ago
ratty-blush

Redirect to same route

When I try to redirect to the same route with a new search object it doesn't keep the search parameter from the redirect. I have tried with my mask and without it.
beforeLoad: async ({ search }) => {
console.log({ beforeLoad: search });
if (search.code && search.state) {
console.log('redirecting');
throw redirect({
to: '.',
search: { showPriority: true },
});
}
},
beforeLoad: async ({ search }) => {
console.log({ beforeLoad: search });
if (search.code && search.state) {
console.log('redirecting');
throw redirect({
to: '.',
search: { showPriority: true },
});
}
},
const search = Route.useSearch();
console.log({ priority: search });
const search = Route.useSearch();
console.log({ priority: search });
Outputs the logs beforeLoad: { code: ..., state: ... } 'redirecting' beforeLoad: { showPriority: true } priority: {}, priority: {},
5 Replies
adverse-sapphire
adverse-sapphire5mo ago
throw redirect({
to: '.',
search: (prev) => ({ ...prev, showPriority: true }),
});
throw redirect({
to: '.',
search: (prev) => ({ ...prev, showPriority: true }),
});
ratty-blush
ratty-blushOP5mo ago
Now that the code and state parameters always exist I am getting an endless loop. Because it is always redirecting with those params. I only want showPriority to go through to the component. Am I supposed to be returning something different from beforeLoad when it has the search params I want?
flat-fuchsia
flat-fuchsia5mo ago
just don't throw a redirect in that case then?
correct-apricot
correct-apricot5mo ago
What's the difference in terms of behavior when you throw a redirect or not?
flat-fuchsia
flat-fuchsia5mo ago
i meant: just throw the redirect if you want to redirect, here this means: if the search param is already present, then dont throw a redirect

Did you find this page helpful?