Skip to content

Can

Declarative conditional render based on a single role, single permission, or multiple permissions with match.

When to use

  • Show/hide UI elements without manual if in the component body.

Signature

tsx
import { Can } from 'advanced-react-role-guard';

<Can role={role} children={...} />
<Can permission={permission} fallback={...} children={...} />
<Can permissions={permissions} match="any" | "all" children={...} />

Props

PropTypeDefaultDescription
rolestringRequire this role.
permissionstringRequire this permission.
permissionsstring[]Require a set of permissions.
match'any' | 'all''any'When using permissions, require any one or all.
fallbackReactNodenullRendered when access is denied.
childrenReactNoderequiredRendered when access is granted.

Behavior

  • Delegates to useCan and renders children if allowed, otherwise fallback.
  • match applies only when permissions is an array.

Edge cases

  • Conflicting props — The implementation passes props through to useCan; prefer one of role, permission, or permissions in practice.
  • Empty permissions arrayhasAnyPermission / hasAllPermissions over an empty list: typically anyfalse, alltrue (vacuous); confirm behavior in your app.

Error / fallback

The component does not throw; denied access renders fallback.

Examples

Simple

tsx
<Can role="admin">
  <AdminPanel />
</Can>

Advanced (fallback + match)

tsx
<Can
  permissions={['order:edit', 'order:cancel']}
  match="all"
  fallback={<span className="muted">No order actions</span>}
>
  <OrderActions />
</Can>

Do / don’t

DoDon’t
Use for UI gatingAssume this hides data from determined users
Use fallback for empty statesUse for server-side authorization

Pitfalls

  • Guard is identical in behavior; use naming that fits your file (see Guard).

Released under the MIT License.