T
TanStack16mo ago
other-emerald

Purpose of `from` for JavaScript users

Hey 👋🏽 , I use tanstack-router in a JS project. Until now I have always tried to supply from in the navigations and then route relative. I.e.:
throw redirect({
from: Route.fullPath,
to: "./step-1",
replace: true,
});
throw redirect({
from: Route.fullPath,
to: "./step-1",
replace: true,
});
What do you recommend to JS users? The relative approach, or the absolute approach?
throw redirect({
to: "/app/create-order/step-1",
replace: true,
});
throw redirect({
to: "/app/create-order/step-1",
replace: true,
});
Thanks
4 Replies
foreign-sapphire
foreign-sapphire16mo ago
My current opinion is if you cannot reliably know that from will match the current location at runtime then use absolute to because it's to a concrete.
other-emerald
other-emeraldOP16mo ago
Ok, do you have an example case, where from would not match the current location?
metropolitan-bronze
metropolitan-bronze16mo ago
this can happen if you have component that is rendered on multiple routes
other-emerald
other-emeraldOP16mo ago
Ah, sure, I didn't even think about that, because I always navigate in the route component. So you can say that if you know where you are, you can prefer relative routing. If you find yourself in a situation where you could pass from as a parameter (e.g. if you use redirect or useNavigate without a route), should you pass from or not? I read here https://github.com/TanStack/router/issues/1665#issuecomment-2131384105 that from does not play a role in runtime. Is that the case? In my opinion, i.e. redirect does not ignore the provided from.

Did you find this page helpful?