React Native Toast Message
React Native Toast Message is a lightweight (~40 kB) and highly customizable animated toast message component designed for React Native applications. It provides an imperative API for easily displaying various types of notifications, including success, error, and info toasts. The current stable version is 2.3.3, with a consistent release cadence focusing on bug fixes, performance improvements, and compatibility updates, as seen with recent fixes for React 19. Key differentiators include its built-in keyboard-aware logic, flexible configuration options, and the ability to define custom toast layouts. The library offers comprehensive documentation for advanced scenarios like integrating toasts within Modals or navigation libraries, which are common pain points in React Native development. It ships with full TypeScript support, ensuring type safety and improved developer experience.
Common errors
-
TypeError: Cannot read property 'show' of undefined
cause The `Toast` object (imperative API) is not correctly imported or available in the scope where `Toast.show()` is called. This often happens if the import is incorrect or if `Toast` is not rendered at the root level in some setups.fixEnsure you are using `import Toast from 'react-native-toast-message';` and that the `<Toast />` component is rendered once at the top level of your React Native application, typically inside your `App.js` or equivalent root component. -
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
cause This error typically occurs when a React component is imported incorrectly, for example, using named import (`{ Toast }`) when it should be a default import (`Toast`).fixChange your import statement for the `Toast` component to use the default export: `import Toast from 'react-native-toast-message';`. -
Toast does not appear or disappears immediately when inside a Modal or Navigation stack.
cause The `Toast` component needs to be rendered at a sufficiently high level in the component hierarchy (e.g., above the Modal or Navigation container) to ensure it renders on top of all other content.fixFollow the specific documentation for 'How to show the Toast inside a Modal?' or 'How to render the Toast when using a Navigation library?' provided in the library's README. Generally, the `<Toast />` component should be placed outside of these elements or inside a dedicated provider/wrapper. -
TypeScript error: Property 'text1' does not exist on type 'ToastOptions' or similar for custom properties.
cause You are trying to use a custom toast type or extend `ToastOptions` with properties that are not part of the default `ToastOptions` interface without proper type declaration.fixIf using a custom type, declare it with `type: 'customType'` and ensure you extend `ToastOptions` with your custom props. For example, `declare module 'react-native-toast-message' { export interface ToastOptions { customProp?: string; } }`.
Warnings
- breaking Major API changes occurred during the migration from v1 to v2. Users upgrading from v1 should consult the complete changelog for v2.0.0 to understand necessary adjustments.
- gotcha Toasts with `autoHide: true` could get stuck when panned or swiped, leading to a persistent toast on screen.
- gotcha On iOS, toasts might not position correctly when the keyboard is open, appearing behind or incorrectly aligned with input fields.
- gotcha Swipeable gestures for dismissing toasts are enabled by default since v2.2.0. This might conflict with other gestures or be undesired in some UX flows.
- gotcha Compatibility issues with React 19 were reported, specifically related to return types for base toast components.
Install
-
npm install react-native-toast-message -
yarn add react-native-toast-message -
pnpm add react-native-toast-message
Imports
- Toast (component & API)
import { Toast } from 'react-native-toast-message';import Toast from 'react-native-toast-message';
- ToastProps
import { ToastOptions } from 'react-native-toast-message';import type { ToastOptions } from 'react-native-toast-message'; - BaseToast, SuccessToast, ErrorToast, InfoToast
import BaseToast from 'react-native-toast-message/BaseToast';
import { BaseToast, SuccessToast } from 'react-native-toast-message';
Quickstart
import React from 'react';
import { Button, View, StyleSheet, SafeAreaView } from 'react-native';
import Toast from 'react-native-toast-message';
const App = () => {
const showSuccessToast = () => {
Toast.show({
type: 'success',
text1: 'Success!',
text2: 'Your operation was completed successfully.',
visibilityTime: 3000,
autoHide: true,
topOffset: 30
});
};
const showErrorToast = () => {
Toast.show({
type: 'error',
text1: 'Error!',
text2: 'Failed to perform operation. Please try again.',
visibilityTime: 4000,
autoHide: true
});
};
return (
<SafeAreaView style={styles.safeArea}>
<View style={styles.container}>
<Button title="Show Success Toast" onPress={showSuccessToast} />
<View style={styles.spacer} />
<Button title="Show Error Toast" onPress={showErrorToast} />
<Toast />
</View>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
safeArea: {
flex: 1,
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
spacer: {
height: 20,
},
});
export default App;