Skip to main content
The WithGuard component provides conditional rendering based on the same guard logic used in route definitions. Use it to control visibility of UI elements like sidebar items, buttons, or entire sections based on user permissions, feature flags, or other criteria.

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

Note: Unlike route guards, 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

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:
Note: The guard result is cached based on 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 use redirectTo() 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