Toast displays alerts on top of an overlay. The Toast terminates itself when the close button is clicked on or after a preset timeout — the default is 5 seconds. The component also allows users to give feedback when an action is completed.
Toasts can also be configured to pop up at different areas of the application window—top or bottom. More than one instance of toast can be present onscreen at one time.
const Example = () => {
const toast = useToast();
return <Button shadow={2} onPress={() => toast.show({
description: 'Hello world'
})}>
Bottom
</Button>;
};
import { useToast } from 'native-base';
const Example = () => {
const toast = useToast();
return <Center>
<Button onPress={() => toast.show({
description: "Hello world"
})}>
Show Toast
</Button>
</Center>;
};
const Example = () => {
const toast = useToast();
return <Center>
<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>
</Center>;
};
Display a custom component instead of the default Toast UI.
const Example = () => {
const toast = useToast();
return <Center>
<Button onPress={() => {
toast.show({
render: () => {
return <Box bg="emerald.500" px="2" py="1" rounded="sm" mb={5}>
Hello! Have a nice day
</Box>;
}
});
}}>
Custom Toast
</Button>
</Center>;
};
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 <Center>
<VStack space={2}>
<Button colorScheme="success" onPress={() => toast.show({
title: "Account verified",
status: "success",
description: "Thanks for signing up with us."
})}>
Success
</Button>
<Button colorScheme="error" onPress={() => toast.show({
title: "Something went wrong",
status: "error",
description: "Please create a support ticket from the support page"
})}>
Error
</Button>
<Button colorScheme="info" onPress={() => toast.show({
title: "Network connection restored",
status: "info",
description: "This is to inform you that your network connectivity is restored"
})}>
Info
</Button>
<Button colorScheme="warning" onPress={() => toast.show({
title: "Invalid email address",
status: "warning",
description: "Please enter a valid email address"
})}>
Warning
</Button>
</VStack>
</Center>;
};
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 <Center>
<Button onPress={() => {
if (!toast.isActive(id)) {
toast.show({
id,
title: "Hey! You can't create a duplicate toast"
});
}
}}>
Click me!
</Button>
</Center>;
}
You can use standalone toast where you don't have access to useToast hook, for example, in a different file, out of a React component.
const Example = () => {
return <Center>
<Button onPress={() => Toast.show({
title: "Hello world"
})}>
Show Toast
</Button>
</Center>;
};
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: "bottom-right" | "bottom" | "top" | "top-right" | "top-left" | "bottom-left"
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`
The variants of the [Alert](alert.md) component.
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 .