

Cross-platform Toast component for React Native
source link: https://reactnativeexample.com/cross-platform-toast-component-for-react-native/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

react-native-fast-toast
Toast component for React Native, supports Android, IOS and Web
Features
- Fully Customizable
- Swipe to close support
- Smooth animation
- Fully typed with TypeScript
Install
Open a Terminal in the project root and run:
yarn add react-native-fast-toast
Usage
Wrap your app in the ToastProvider
, which provides context for the Toast hook.
import { ToastProvider } from 'react-native-fast-toast'
export default function App() {
return (
<ToastProvider>
<RestOfYourApp />
<ToastProvider/>
);
}
Then use hook like this everywhere in your app:
import { useToast } from 'react-native-fast-toast'
const Component = () => {
const toast = useToast()
useEffect(() => {
toast.show("Hello World")
}, [])
}
Methods
show()
toast.show("Task finished successfully", {
type: 'normal | success | warning | danger | custom',
position: 'top | bottom',
duration: 4000,
offset: 30,
animationType: 'slide-in | zoom-in'
});
update()
let id = Math.random().toString()
toast.show("Loading...", {id})
toast.update(id, "Loading compeleted");
hide()
let id = Math.random().toString()
toast.show("Loading...", {id})
toast.hide(id);
// or
toast.hideAll()
Customization
ToastContainer
props
There are lots of props to customize your toast or your can use renderToast to implement your own component.
<ToastContainer
placement="bottom | top"
duration={5000}
animationType='slide-in | zoom-in'
animationDuration={250}
successColor="green"
dangerColor="red"
warningColor="orange"
normalColor="gray"
icon={<Icon />}
successIcon={<SuccessIcon />}
dangerIcon={<DangerIcon />}
warningIcon={<WarningIcon />}
textStyle={{ fontSize: 20 }}
offset={50}
renderToast={(toastOptions) => JSX.Element} implement custom toast component.
/>
Custom toast types
You can implement your own custom types or overwrite the existing ones
<ToastContainer
renderType={{
custom_type: (toast) => (
<View style={{padding: 15, backgroundColor: 'grey'}}>
<Text>{toast.message}</Text>
</View>
)
}}
/>
// You can pass other data to your custom toast using data property in show method.
toast.show("Show custom toast", {data: { title: 'Toast title' }})
- How to call toast outside React components?
To call toasts everywhere (even outside of React components like in redux actions), do this in root component of your app (index.js or App.js)
import Toast from "react-native-fast-toast";
export default function App() {
return (
<>
<RestOfYourApp />
<Toast ref={(ref) => global['toast'] = ref} />
</>
);
Now you can call toast.show()
everywhere on app. similar to alert
.
TypeScript Note: add index.d.ts to your project root.
- How to show toast inside a Modal?
The Modal component is a native view that sits on top of the rest of react-native application. The only way to put something above it is to put something in the modal itself, or alternately to use a JS only implementation of a Modal.
As a workaround you can put toast inside modal like this:
import Toast from "react-native-fast-toast";
export Component = () => {
const toastRef = useRef();
return (
<Modal>
.....
<Toast ref={toastRef} />
</Modal>
}
GitHub
Recommend
-
31
Tabs A cross-platform Collapsible Tab View component for React Native Dec 03, 2020...
-
20
@benjeau/react-native-draw Cross-platform React Native drawing component based on SVG. Example applic...
-
8
react-native-fast-toast A Toast component for react-native, supports Android, IOS, Web, Windows. Features Normal, Success, Danger and Warning toasts Customizable and Icon support Smooth animatio...
-
14
react-native-clipboard-toast React Native Clipboard API with Animated toast message component. Support both Android and iOS | Used react native Clipboard | Toast by calling api.
-
47
Search A cross-platform selector/picker component for React Native May 07, 2021...
-
22
In this article, we will discuss how to enable your app to show toast notifications, which you can use to notify the user that something needs their attention, or to notify them that a long running operation has completed. Create a n...
-
5
@parmeetsasijaParmeet Singh AsijaFreelance Frontend Developer with 2+ years experience in React, Javascript and Typescript | Software Engineer
-
12
React Native Toast A toast library for react-native, built on react-hot-toast. It supports features such as multiple toasts, keyboard handling, swipe to dismiss, positional to...
-
9
Apps A high-performance, cross-platform animated header component for React Native applications Mar 26, 2023 2 min read
-
5
Toast A React Native component that displays a toast message on the screen Apr 26, 2023 2 min read
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK