Code Monkey home page Code Monkey logo

react-native-toast-notifications's Introduction

react-native-toast-notifications

Version MIT License

Toast component for React Native, supports Android, IOS and Web

Features

  • Fully Customizable
  • Swipe to close support
  • Smooth animation
  • Fully typed with TypeScript

Demo

react-native-web Demo

Install

Open a Terminal in the project root and run:

yarn add react-native-toast-notifications

Usage

Wrap your app in the ToastProvider, which provides context for the Toast hook.

import { ToastProvider } from 'react-native-toast-notifications'

export default function App() {
  return (
    <ToastProvider>
      <RestOfYourApp />
    <ToastProvider/>
  );
}

Then use hook like this everywhere in your app:

import { useToast } from "react-native-toast-notifications";

const Component = () => {
  const toast = useToast();

  useEffect(() => {
    toast.show("Hello World");
  }, []);
};

Methods

show()

toast.show("Task finished successfully", {
  type: "normal | success | warning | danger | custom",
  placement: "top | bottom",
  duration: 4000,
  offset: 30,
  animationType: "slide-in | zoom-in",
});

update()

let id = toast.show("Loading...");
toast.update(id, "Loading completed", {type: "success"});

hide()

let id = toast.show("Loading...");
toast.hide(id);
// or
toast.hideAll();

Customization

ToastProvider props

There are lots of props to customize your toast or your can use renderToast to implement your own component.

<ToastProvider
    placement="bottom | top"
    duration={5000}
    animationType='slide-in | zoom-in'
    animationDuration={250}
    successColor="green"
    dangerColor="red"
    dangerColor="red"
    warningColor="orange"
    normalColor="gray"
    icon={<Icon />}
    successIcon={<SuccessIcon />}
    dangerIcon={<DangerIcon />}
    warningIcon={<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.
>
...
</>

Custom toast types

You can implement your own custom types or overwrite the existing ones

<ToastProvider
    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' }})

FAQ

- 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-toast-notifications";

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-toast-notifications";

export Component = () => {
    const toastRef = useRef();
    return (
        <Modal>
            .....
            <Toast ref={toastRef} />
        </Modal>

}

Contributing

Pull request are welcome.

While developing, you can run the example app to test your changes.

Donation

If this project helped you reduce time to develop, you can buy me a cup of coffee :)

Buy Me A Coffee

Hire

Looking for a React/React-Native Expert? Email at [email protected]

License

MIT

react-native-toast-notifications's People

Contributors

arnnis avatar arthurdenner avatar dependabot-preview[bot] avatar devthejo avatar jcgertig avatar lukeramsden avatar octabruzzesi avatar rayliao avatar xagustin93 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.