Skip to main content

useToast

useToast is a hook that shows and dismisses notifications, such as confirming a save or closing a sync message when work finishes.

Import

import { useToast } from '@vortexlabs/vortex-native';
import { Button, useToast } from "@vortexlabs/vortex-native";

export const ToastNotification = () => {
  const { show } = useToast();

  return (
    <Button onPress={() => show({ title: "Link copied", tone: "success" })}>
      Show copy confirmation
    </Button>
  );
};

Features

  • Queues a notification and returns its ID.
  • Dismisses a notification by ID.
  • Uses the closest toast provider.

Notification IDs

Keep the ID returned by show when you need to dismiss the message from another handler. Here, finishing the sync dismisses the persistent message and shows a confirmation.

import { Button, Stack, useToast } from "@vortexlabs/vortex-native";
import React from "react";

export const ToastNotificationId = () => {
  const toastId = React.useRef<string | null>(null);
  const { show, hide } = useToast();

  return (
    <Stack gap={2}>
      <Button onPress={() => {
        if (toastId.current) hide(toastId.current);
        toastId.current = show({ title: "Syncing messages", timeout: 0 });
      }}>
        Start sync
      </Button>
      <Button variant="secondary" onPress={() => {
        if (toastId.current) {
          hide(toastId.current);
          toastId.current = null;
          show({ title: "Messages synced", tone: "success" });
        }
      }}>
        Finish sync
      </Button>
    </Stack>
  );
};

Provider Scope

Call the hook in a component inside VortexProvider, which supplies a toast provider. A nested ToastProvider creates its own queues. Call show and hide from the provider that owns the notification.

See Toast for notification options, actions, and placement.