Guard

Renders its children unless a condition is true. Then it renders nothing, or an alternative.

Usage

When guardIf is true, thenRender takes the place of the children.

tsx
<Guard guardIf={!user} thenRender={SignInPrompt}>
  <Dashboard user={user} />
</Guard>

Sign in to see your dashboard.

Hiding

When guardIf is true, Guard shows thenRender instead of its children—or nothing if you pass no alternative. shouldHide takes precedence and renders nothing.

tsx
<Guard guardIf={items.length === 0} shouldHide>
  <ItemList items={items} />
</Guard>

Deferred children

Children written as JSX are created even when they are guarded. Pass a function when they read something that only exists in the unguarded case.

tsx
<Guard guardIf={!invoice}>{() => <Total amount={invoice!.total} />}</Guard>

Reference

proptypedefault
guardIf?booleanfalsetrue hides the children
thenRender?Renderablenullrendered in place of the children
shouldHide?booleanfalserenders nothing, even with thenRender
children?Renderable—rendered while guardIf is false