If you are starting a new project, please try gluestack-ui for better performance and new design.
Toast
Toast displays alerts on top of an overlay. The Toast terminates itself when the close button is clicked 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.
Show Code
Playground
constExample=()=>{
const toast =useToast();
return<Buttonshadow={2}onPress={()=> toast.show({
description:'Hello world'
})}>
Bottom
</Button>;
};
Import
Copy
import{ useToast }from'native-base';
Examples
Basic
Playground
constExample=()=>{
const toast =useToast();
return<Center>
<ButtononPress={()=> toast.show({
description:"Hello world"
})}>
Show Toast
</Button>
</Center>;
};
Position
Playground
constExample=()=>{
const toast =useToast();
return<Center>
<VStackspace={2}>
<ButtononPress={()=> toast.show({
title:"Hello world",
placement:"bottom"
})}>
Bottom
</Button>
<ButtononPress={()=> toast.show({
title:"Hello world",
placement:"top"
})}>
Top
</Button>
<ButtononPress={()=> toast.show({
title:"Hello world",
placement:"top-left"
})}>
Top left
</Button>
<ButtononPress={()=> toast.show({
title:"Hello world",
placement:"top-right"
})}>
Top right
</Button>
<ButtononPress={()=> toast.show({
title:"Hello world",
placement:"bottom-left"
})}>
Bottom left
</Button>
<ButtononPress={()=> toast.show({
title:"Hello world",
placement:"bottom-right"
})}>
Bottom right
</Button>
</VStack>
</Center>;
};
Custom component
Display a custom component instead of the default Toast UI.
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(...).
Playground
functionExample(){
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>;
}
Standalone Toast
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.
Playground
constExample=()=>{
return<Center>
<ButtononPress={()=> Toast.show({
title:"Hello world"
})}>
Show Toast
</Button>
</Center>;
};
Props
The following props can be passed while calling toast.show.