Toast is used to show alerts on top of an overlay. Toast will close itself when the close button is clicked on or after a timeout — the default is 5 seconds. The toast component is used to give feeback to users after an action has taken place.
Toasts can be configured to appear at either the top or the bottom of an application window, and it is possible to have more than one toast onscreen at a time.
import { useToast } from 'native-base';
const Example = () => {
const toast = useToast();
return <Button onPress={() => toast.show({
title: "Hello world"
})}>
Bottom
</Button>;
};
const Example = () => {
const toast = useToast();
return <VStack space={2}>
<Button onPress={() => toast.show({
title: "Hello world",
placement: "bottom"
})}>
Bottom
</Button>
<Button onPress={() => toast.show({
title: "Hello world",
placement: "top"
})}>
Top
</Button>
<Button onPress={() => toast.show({
title: "Hello world",
placement: "top-left"
})}>
Top left
</Button>
<Button onPress={() => toast.show({
title: "Hello world",
placement: "top-right",
backgroundColor: "pink.400"
})}>
Top right
</Button>
<Button onPress={() => toast.show({
title: "Hello world",
placement: "bottom-left"
})}>
Bottom left
</Button>
<Button onPress={() => toast.show({
title: "Hello world",
placement: "bottom-right"
})}>
Bottom right
</Button>
</VStack>;
};
Display a custom component instead of the default Toast UI.
const Example = () => {
const toast = useToast();
return <Button onPress={() => {
toast.show({
render: () => {
return <Box bg="teal.500" px={4} py={3} rounded="md" mb={5}>
Hi, Nice to see you ( ´ ∀ ` )ノ
</Box>;
}
});
}}>
Custom Toast
</Button>;
};
Toasts can be closed imperatively, individually (via the close instance method), or all together (via the closeAll instance method).
const Example = () => {
const toast = useToast();
const toastIdRef = React.useRef();
function close() {
if (toastIdRef.current) {
toast.close(toastIdRef.current);
}
}
function closeAll() {
toast.closeAll();
}
function addToast() {
toastIdRef.current = toast.show({
title: "Hi, Nice to see you ( ´ ∀ ` )ノ"
});
}
return <Stack direction={{
base: "column",
md: "row"
}} space={2}>
<Button onPress={addToast}>Toast</Button>
<Button onPress={close} variant="outline">
Close last toast
</Button>
<Button onPress={closeAll} variant="outline">
Close all toasts
</Button>
</Stack>;
};
You can use status to change the color of your toasts.
Toast uses the same variants as the
component.
const Example = () => {
const toast = useToast();
return <VStack space={2}>
<Button onPress={() => toast.show({
title: "Account verified",
status: "success",
description: "Thanks for signing up with us."
})}>
Success
</Button>
<Button onPress={() => toast.show({
title: "Something went wrong",
status: "error",
description: "Please create a support ticket from the support page"
})}>
Error
</Button>
<Button onPress={() => toast.show({
title: "Network connection restored",
status: "info",
description: "This is to inform you that your network connectivity is restored"
})}>
Info
</Button>
<Button onPress={() => toast.show({
title: "Invalid email address",
status: "warning",
description: "Please enter a valid email address"
})}>
Warning
</Button>
</VStack>;
};
Preventing Duplicate Toast
In some cases, you might need to prevent duplicates of specific toasts. To achieve this, you need to pass an id and use the toast.isActive method to determine when to call toast.show(...).
function Example() {
const toast = useToast();
const id = "test-toast";
return <Button onPress={() => {
if (!toast.isActive(id)) {
toast.show({
id,
title: "Hey! You can't create a duplicate toast"
});
}
}}>
Click me!
</Button>;
}
The following props can be passed while calling toast.show.
Toast implements
,
,
,
,
,
,
,
,
,
,
,
,
The title to be rendered in the Toast
The description of the toast
The delay before the toast hides (in milliseconds). If set to `null`, toast will never dismiss.
The `id` of the toast. Mostly used when you need to prevent duplicate. By default, we generate a unique `id` for each toast
If `true`, toast will show a close button
Callback function to run side effects after the toast has closed.
The placement of the toast. Defaults to bottom
Type: "top" | "top-right" | "top-left" | "bottom" | "bottom-left" | "bottom-right"
Render a component toast component. Any component passed will receive 2 props: `id` and `onClose`.
Type: (props: any) => ReactNode
The status of the toast. Adding status will render an [Alert](alert.md) component inside the `Toast`
Type: "info" | "warning" | "error" | "success"
The variants of the [Alert](alert.md) component.
Type: "subtle" | "solid" | "left-accent" | "top-accent" | "outline" | "outline-light"
accessibilityAnnouncement
The text to be announced by a screen reader when the Toast opens.
Determines the [accessibility announcement tone](https://reactnative.dev/docs/accessibility#accessibilityliveregion-android).
Type: "none" | "polite" | "assertive"
On Android and Web, Toast renders under a View with accessibilityLiveRegion which announces the content rendered inside it to screen reader devices.
On iOS, accessibilityLiveRegion is not supported yet, so we use the
to announce the content.
NativeBase ships with a default theme for each component. Check out the default theme of the toast
.
Info:
We can easily extend the toast component theme using extendTheme function as described in the documentation .