redirectTo() is a helper function that returns a guard result indicating the user should be redirected to a different path.
Type Signature
Parameters
string
required
The path to redirect to. Can be absolute (e.g.,
/login) or relative (e.g., settings/general).Behavior
When a guard returnsredirectTo(path):
- Client-side redirect: The user is navigated to the specified path
- Guard chain stops: Subsequent guards are not evaluated
- No component render: The original route component does not render
- Navigation history: The redirect is added to browser history
Basic Usage
Authentication Redirect
Most common use case - redirect unauthenticated users to login:Module Without Component
UseredirectTo() to create modules that automatically redirect to a specific resource:
Common Use Cases
Onboarding Flow
Redirect users who haven’t completed onboarding:Role-Based Redirect
Redirect users based on their role:Subscription Check
Redirect users without active subscription:Default Route Redirect
Create a root path that redirects to a default location:Async Redirect (Feature Flag)
When to Use redirectTo() vs hidden()
Use redirectTo() when:
- User needs authentication (redirect to login)
- User needs to complete a required step (onboarding, subscription)
- You want to guide users to the correct page based on state
- The route exists but is not yet accessible (redirect to “coming soon”)
Use hidden() when:
- The route should appear to not exist for unauthorized users
- Access is based on roles, permissions, or feature flags
- You want to prevent route discovery
- The user should not know the feature exists
Example: Combined Guards
Absolute vs Relative Paths
Absolute Paths (Recommended)
Start with/ to navigate from the app root:
Relative Paths
Relative to the current basePath:Return Value
redirectTo(path) returns a GuardResult object:
Redirect with State (Advanced)
For advanced redirect scenarios, you can use React Router’s redirect function directly:See Also
- Guards Overview - Understanding guards
- pass() - Allow access to routes
- hidden() - Deny access and show 404