[![Version][version-badge]][package] [![MIT License][license-badge]][license]
sh
yarn add react-native-toast-notifications
`
Usage
Wrap your app in the ToastProvider, which provides context for the Toast hook.
`js
import { ToastProvider } from 'react-native-toast-notifications'
export default function App() {
return (
);
}
`
Then use hook like this everywhere in your app:
`js
import { useToast } from "react-native-toast-notifications";
const Component = () => {
const toast = useToast();
useEffect(() => {
toast.show("Hello World");
}, []);
};
`
Methods
$3
`js
toast.show("Task finished successfully", {
type: "normal | success | warning | danger | custom",
placement: "top | bottom",
duration: 4000,
offset: 30,
animationType: "slide-in | zoom-in",
});
`
$3
`js
let id = toast.show("Loading...");
toast.update(id, "Loading completed", {type: "success"});
`
$3
`js
let id = toast.show("Loading...");
toast.hide(id);
// or
toast.hideAll();
`
Customization
$3
There are lots of props to customize your toast or your can use renderToast to implement your own component.
`js
placement="bottom | top"
duration={5000}
animationType='slide-in | zoom-in'
animationDuration={250}
successColor="green"
dangerColor="red"
dangerColor="red"
warningColor="orange"
normalColor="gray"
icon={ }
successIcon={ }
dangerIcon={ }
warningIcon={ }
textStyle={{ fontSize: 20 }}
offset={50} // offset for both top and bottom toasts
offsetTop={30}
offsetBottom={40}
swipeEnabled={true}
renderToast={(toastOptions) => JSX.Element} implement custom toast component.
>
...
>
`
$3
You can implement your own custom types or overwrite the existing ones
`js
renderType={{
custom_type: (toast) => (
{toast.message}
)
}}
>
...
>
// You can pass other data to your custom toast using data property in show method.
toast.show("Show custom toast", {data: { title: 'Toast title' }})
`
FAQ
$3
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)
`js
import Toast from "react-native-toast-notifications";
export default function App() {
return (
<>
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.
$3
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-toast-notifications";
export Component = () => {
const toastRef = useRef();
return (
.....
}
``