Toast
A temporary notification component for displaying feedback to users.
Toast
The Toast component provides a non-intrusive way to display short feedback messages to users. It appears at the top of the screen and automatically disappears after a specified duration.
Features
- Multiple Types: Support for success, error, info, and warning toast types
- Customizable Duration: Control how long the toast appears
- Animated Transitions: Smooth enter and exit animations
- Theming Support: Automatically adapts to light/dark mode
- Callback Support: Execute code when the toast disappears
Import
Props
Prop | Type | Default | Description |
---|---|---|---|
message | string | Required | The message to display in the toast |
type | 'success' | 'error' | 'info' | 'warning' | 'info' | The type of toast to display |
duration | number | 3000 | Time in milliseconds before the toast auto-hides |
onHide | () => void | — | Callback function called when toast is hidden |
isVisible | boolean | Required | Controls the visibility of the toast |
Usage Examples
Basic Toast
Different Toast Types
Custom Duration Toast
Toast Service Example
Creating a Toast service for your application:
Using the Toast service in your components:
Form Submission Example
Best Practices
Keep Messages Concise
Toast messages should be short and to the point:
Use Appropriate Toast Types
Choose the right toast type for the message:
Avoid Overusing Toasts
Only show toasts for important information:
Implementation Details
The Toast component uses React Native's Animated API to create smooth slide-in and slide-out animations. It positions itself at the top of the screen using absolute positioning and has a high z-index to ensure it appears above other content.
The component uses a colored dot to indicate the toast type and automatically adapts its text and background colors based on the current theme using the useThemeColors
hook.
When isVisible
becomes true, the component animates in, waits for the specified duration, and then animates out. Once the exit animation completes, it calls the onHide
callback, which typically updates the parent component's state to set isVisible
to false.
Notes
- Multiple toast messages will stack and may overlap if not managed properly
- Toast animations are handled using React Native's Animated API for smooth performance
- The component uses a colored indicator dot to differentiate between toast types
- For a centralized toast system, consider implementing a context-based solution
- When using with navigation, make sure to place the Toast component at the root level