🔧 Programmierung 🕛 vor 11 Monaten 6 Min Lesezeit
0

TanStack Router: Go to Previous page after Sign In

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht
📺
dev.to

Welcome back to the TanStack Router series, today going double digits with






The problem in one sentence



You sign in and get redirected to the homepage, losing the page and search parameters you had before.






Approach 1: explicit redirect via search param



This is the straightforward and reliable solution. We explicitly pass a redirectTo param everywhere the user can navigate to the sign in page and we use it to go back after signing in.



Our Sign In form component will look like this, it defaults to the homepage or redirects to the provided URL after a successful sign in.




CODE
export const SignInForm = ({ redirectTo = '/' }: { redirectTo?: string }) => {
const navigate = useNavigate();

const signIn = () => {
// Your sign in logic here

navigate({ to: redirectTo });
};

return (
<form onSubmit={signIn}>
{/* form fields */}
<Button type="submit">Sign In</Button>
</form>
);
};





But where does redirectTo come from? Let's add it as a search parameter to the sign in route.



CODE
import { createFileRoute } from '@tanstack/react-router';
import { SignInForm } from 'src/components/auth/sign-in-form';
import { Layout } from 'src/components/layout';
import { z } from 'zod';

export const Route = createFileRoute('/sign-in')({
component: RouteComponent,
validateSearch: z.object({
redirectTo: z.string().optional().catch('/'),
}),
// ...
});

function RouteComponent() {
const { redirectTo } = Route.useSearch();

return (
<Layout>
{/* ... */}
<SignInForm redirectTo={redirectTo} />
{/* ... */}
</Layout>
);
}





What is that validateSearch doing? It uses Zod to parse the search parameters and extract redirectTo, defaulting it to / if not provided. Then in the component we read it with Route.useSearch() and pass it down to the SignInForm.




If you want to learn more about handling search parameters with TanStack Router, check out my previous article:




With this, after a successful sign in you will land back on the original page including all the search parameters.





Tradeoff of the explicit approach



You must remember to add the redirectTo param in every link to the sign in page. Not a big issue though, and you can wrap it in a helper or custom hook like a "go to sign in" utility, but it is still one more thing to do.





Approach 2: capture previous location automatically



I also experimented with a small hook that tracks the previous location without passing any search parameters to the sign in page. I'm not entirely sure how stable and reliable this is, but it worked well in my tests so I thought it was worth sharing:



CODE
function usePreviousLocation() {
const router = useRouter();
const [previousLocation, setPreviousLocation] = useState<string>('/');
useEffect(() => {
return router.subscribe('onResolved', ({ fromLocation }) => {
setPreviousLocation(fromLocation?.href ?? '/');
});
}, []);
return previousLocation;
}





This hook subscribes to the router's navigation events and captures the "from" location after each navigation. It stores it in state so it can be used later.



With this alone you can forget about everything we already said, you'll no longer need to pass redirectTo in each navigation or handle search params. Just use the hook in your sign in form:



CODE
export const SignInForm = () => {
const navigate = useNavigate();
const previousLocation = usePreviousLocation();

const signIn = () => {
// Your sign in logic here

navigate({ to: previousLocation });
};

return (
<form onSubmit={signIn}>
{/* form fields */}
<Button type="submit">Sign In</Button>
</form>
);
};







Combine both for a robust UX



The two approaches might also work well together. You can prefer the explicit redirectTo when provided, and fall back to the previously captured location when it is not. This covers both deliberate redirects and default behavior without extra effort.



CODE
export const SignInForm = ({ redirectTo }: { redirectTo?: string }) => {
const navigate = useNavigate();
const previousLocation = usePreviousLocation();

const signIn = () => {
// Your sign in logic here

navigate({ to: redirectTo ?? previousLocation });
};

return (
<form onSubmit={signIn}>
{/* form fields */}
<Button type="submit">Sign In</Button>
</form>
);
};







Notes




  • This article focuses on TanStack Router, but the same idea works in TanStack Start too.

  • If you are using authenticated routes or guards, you might also like this related article: , you can find it here:



    Do you like my content? You might consider subscribing to my YouTube channel! It means a lot to me ❤️

    You can find it here:







Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
2 Quellen
CVE-2024-45058 | portabilis i-educar up to 2.8 Setting educar_usuario_cad.php authorization
1 Quelle
Kompakte 10.000-mAh-Powerbank für weniger als 10 Euro bei Amazon Haul
1 Quelle
Bessere Grafik in Spielen: So steigern Sie die Bildqualität ohne FPS-Verlust
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten TanStack Router: Go to Previous page after Sign In

Thematisch verwandte Begriffe: TanStack, Router, Previous, page · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...