React Native Flash Message
react-native-flash-message is a widely used utility module for React Native that enables developers to easily create and display highly customizable flashbars, top notifications, and alerts. It is currently in a stable state with version 0.4.2, actively maintained to ensure compatibility with recent iPhone devices, including robust support for display notches (like iPhone X, XR, XS, and XS Max). The library offers both global and local instantiation patterns for its `FlashMessage` component, allowing flexible integration into various application architectures. Its key differentiators include ease of use, extensive customization options for message types (info, success, warning, danger, default), and critical attention to UI/UX details like safe area handling on modern mobile devices. The project has a consistent, though not strictly scheduled, release cadence, primarily focusing on compatibility and bug fixes.
Common errors
-
TypeError: Cannot read property 'showMessage' of undefined
cause The `showMessage` function was called without an instantiated global `FlashMessage` component, or it was not imported correctly.fixEnsure `import { showMessage } from 'react-native-flash-message';` is present and that a `<FlashMessage />` component is rendered as the last child in your root component tree. -
Error: `message` attribute is required for FlashMessage
cause The object passed to the `showMessage` function is missing the mandatory `message` property.fixAdd a `message` string property to the object you pass to `showMessage`, for example: `showMessage({ message: 'Your notification text here', type: 'info' })`. -
Flash messages are not appearing or are obscured by other UI elements.
cause The `FlashMessage` component is not placed as the last child in the main application's render tree or within its intended parent `View` component.fixMove the `<FlashMessage />` component to be the absolute last element rendered within your root `View` component in your `App.js`/`App.tsx` file.
Warnings
- breaking In version `0.4.0`, the internal dependency for iPhone notch and safe area detection was changed from the unmaintained `react-native-iphone-x-helper` to `react-native-iphone-screen-helper`. While this is primarily an internal change, projects with existing, specific workarounds or other dependencies relying on the older helper might experience subtle layout regressions on notch devices if not properly updated.
- gotcha The `FlashMessage` component *must* be rendered as the very last child within its parent `View` to ensure it correctly overlays all other content and is fully visible. Incorrect placement will often lead to messages being obscured by other UI elements.
- gotcha When calling the `showMessage` function, the message object passed as an argument *must* include a `message` property with a string value. Omitting this required property will result in an error or an empty flash message failing to display.
- gotcha For local `FlashMessage` instances scoped to a specific screen or component (instead of global), the `FlashMessage` component must be rendered with a `ref` prop. This allows you to call `showMessage` directly on that specific instance via the ref.
Install
-
npm install react-native-flash-message -
yarn add react-native-flash-message -
pnpm add react-native-flash-message
Imports
- FlashMessage
import { FlashMessage } from 'react-native-flash-message';import FlashMessage from 'react-native-flash-message';
- showMessage
import showMessage from 'react-native-flash-message';
import { showMessage } from 'react-native-flash-message'; - hideMessage
import hideMessage from 'react-native-flash-message';
import { hideMessage } from 'react-native-flash-message';
Quickstart
import React from "react";
import { View, Button, StyleSheet, Text } from "react-native";
import FlashMessage, { showMessage } from "react-native-flash-message";
function App() {
return (
<View style={styles.container}>
<Text style={styles.title}>React Native Flash Message Demo</Text>
<Button
title="Show Info Message"
onPress={() => {
showMessage({
message: "Hello from Flash Message!",
description: "This is an example info message.",
type: "info",
duration: 3000,
icon: "success"
});
}}
/>
<View style={{ height: 20 }} />
<Button
title="Show Error Message"
color="red"
onPress={() => {
showMessage({
message: "Something went wrong!",
description: "Please check your network connection and try again.",
type: "danger",
duration: 5000,
icon: "danger",
position: "bottom"
});
}}
/>
{/* GLOBAL FLASH MESSAGE COMPONENT INSTANCE - MUST BE THE LAST COMPONENT */}
{/* This component acts as the container for all flash messages. */}
{/* It should be placed at the root level of your app, as the very last child. */}
<FlashMessage position="top" />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: "center",
alignItems: "center",
padding: 20,
backgroundColor: "#f0f0f0"
},
title: {
fontSize: 22,
fontWeight: "bold",
marginBottom: 40,
textAlign: "center"
}
});
export default App;