TanStackT
TanStack2y ago
2 replies
brilliant-lime

Best practice for redirecting after a fetch

Hi everyone,

I'm working on a routing setup and need some guidance on the best way to handle a redirect after fetching data. Here's my scenario:

1. I have two routes: /users and /users/$userId
2. The purpose of the /users route is to redirect to /users/$userId for the first user fetched from my backend.
3. I've attempted to use throw redirect() in the loader, but it's not working as expected due to preloading.

Here's what I'm trying to achieve:
- When a user visits /users, the app should fetch the first user from the backend.
- Then, it should immediately redirect to /users/$userId where $userId is the ID of the first user fetched.

I'd appreciate any suggestions on how to implement this efficiently and correctly. Are there any best practices or specific methods I should be using for this kind of redirect after a fetch? Should I do in the component ?

Thanks in advance for your help!
Was this page helpful?