Skip to content
React NativeBeta

Alert

Inline messages with an intent, actions and dismiss.

Beta. React Native support is in beta. APIs may change or break between prereleases (npm tag native).

On this page
Web preview — real device may differ
tsx
import { useState } from "react";
import { Alert } from "@/component-lib/alert";
import { Button } from "@/component-lib/button";
import { Section } from "./layout"; // Section and Row: page layout for the demo
 
const VARIANTS = ["info", "success", "warning", "danger", "neutral"] as const;
 
export function AlertDemo() {
  const [shown, setShown] = useState(true);
  return (
    <>
      <Section title="Alert · variants">
        {VARIANTS.map((variant) => (
          <Alert
            key={variant}
            variant={variant}
            title={`${variant[0].toUpperCase()}${variant.slice(1)}`}
            description="A message that stays on the page until the state changes."
          />
        ))}
      </Section>
 
      <Section title="Alert · small, actions, dismiss">
        <Alert size="sm" variant="success" description="3 rows imported." />
        <Alert size="sm" variant="neutral" icon={false} description="Read-only: this invoice is locked." />
        <Alert
          variant="danger"
          title="Payment declined"
          description="The card ending 4242 was declined by the bank."
          actions={
            <>
              <Button size="sm" variant="danger">Try again</Button>
              <Button size="sm" variant="ghost">Use another card</Button>
            </>
          }
        />
        {shown ? (
          <Alert variant="info" title="Trial ends in 3 days" onDismiss={() => setShown(false)}>
            Upgrade to keep your saved views.
          </Alert>
        ) : (
          <Button size="sm" variant="outline" onPress={() => setShown(true)}>
            Show dismissed alert
          </Button>
        )}
      </Section>
    </>
  );
}

Install

npx @grampro/kit@native add Alert

Usage

tsx
import { Alert } from "@/component-lib/alert";
 
<Alert variant="danger" title="Payment declined" description="Try another card." onDismiss={hide} />

Props

AlertProps

PropTypeDescription
variantAlertVariantDefault info.
sizeAlertSizeDefault md.
titleReactNode
descriptionReactNodeThe message. children works too.
childrenReactNode
iconReactNode | falseReplace the variant's glyph, or pass false for none.
actionsReactNodeButtons under the message: "Try again", "Go to billing".
onDismiss() => voidShow a close button. The alert does not hide itself; remove it in the handler.
styleStyleProp<ViewStyle>
stylesPartial<{ root: StyleProp<ViewStyle>; icon: StyleProp<ViewStyle>; content: StyleProp<Vi…
localeTextPartial<AlertLocaleText>

Also accepts the props of Omit<ViewProps, "children" | "style" | "role">.

Native and web

  • aria-live is Android only; iOS needs AccessibilityInfo.announceForAccessibility for appearing alerts.
  • Web version: Alert on the web.