Import
Basic Usage
Props
Guard[]
required
Array of guard functions to evaluate. All guards must return
pass() for children to render.Guards are evaluated in order, and evaluation stops at the first non-pass result.See Route Guards for guard function reference.React.ReactNode
required
Content to render when all guards pass
React.ReactNode
default:"null"
Content to render when any guard returns
hidden()React.ReactNode
default:"null"
Content to render while async guards are being evaluated
Guard Functions
WithGuard uses the same guard types as route guards. See the Route Guards documentation for complete details.Guard Type
redirectTo() is not supported in WithGuard. Use hidden() with a fallback that handles navigation if needed.
Helper Functions
Common Use Cases
Role-Based Access Control
Feature Flags
Sidebar Navigation
Control visibility of navigation items:Subscription-Based Access
Multiple Guards
All guards must pass for children to render:Parameterized Guards
Create reusable guard factories:Async Guards
WithGuard supports async guards with Suspense integration:contextData reference equality. Guards re-evaluate only when contextData changes.
Context Data Access
Guards receive the context data passed to AppShell:Comparison with Route Guards
Best Practices
1. Reuse Route Guards
Define guards once and use them in both routes and UI:2. Avoid Redirects in WithGuard
Don’t useredirectTo() in WithGuard guards. Handle navigation in the fallback:
3. Provide Meaningful Fallbacks
Show helpful messages instead of hiding content silently:4. Handle Loading States
Provide loading indicators for async guards:5. Combine with Error Boundaries
Wrap WithGuard in error boundaries for async guard failures:TypeScript
Related
- Route Guards - Full guard function reference
- AppShell - Defining context data types
- Guards & Permissions - Using guards in navigation