CanAccess is a component that wraps content and conditionally renders it based on access control permissions. It internally uses the useCan hook to check permissions.
Usage
Props
Examples
Basic Usage
Conditionally render an edit button:With Fallback
Show a message when access is denied:With onUnauthorized Callback
Log unauthorized access attempts:Auto-detecting Resource and Action
When used within a Refine page, resource and action can be inferred from the route:Passing Props to Children
Additional props are cloned to the child element:Multiple Actions
Check multiple permissions:With Custom Parameters
Pass additional context to your access control provider:Complex Fallback UI
Render rich fallback content:Nested CanAccess Components
Create complex permission hierarchies:With Custom Query Options
Behavior
- When
canreturnstrue, thechildrenare rendered - When
canreturnsfalse:- The
fallbackis rendered if provided - The
onUnauthorizedcallback is called if provided nullis rendered if no fallback is provided
- The
- If the child is a valid React element, additional props passed to
CanAccessare cloned to the child - If the child is not a valid React element (e.g., a string or fragment), it’s wrapped in a fragment
- During the permission check, nothing is rendered (returns
null)
Notes
- The component uses
useCaninternally, so all the caching and query behavior from TanStack Query applies - If no
resourceoractionprops are provided, they will be inferred from the current route usinguseResourceParams - The
onUnauthorizedcallback is triggered viauseEffectwhendata?.canbecomesfalse - If no access control provider is configured, the component will render its children by default