Git Product home page Git Product logo

react-toast-notification's Introduction

react-toast-notification

Edit react-typescript

Usage

Wrap your application with Toaster

If you want to send toasts from anywhere in the application, you need to first wrap your application with it. Inside, we use context and custom hooks to allow triggering new toasts.

import Toaster from "./Toaster";

const App = () => {
  return <Toaster>...</Toaster>;
};

useToast anywhere in the application

If you want to send toasts from anywhere in the application, you need to first wrap your application with it. Inside, we use context and custom hooks to allow triggering new toasts.

type accepts success, warning and danger.

Pass any message, it will work fine with one line but also adapt with height for longer messages.

If you do not specify duration, it will default to 6 seconds (6000ms).

import { useToast } from "./Toaster/hooks";

const MyComponent = () => {
  const { triggerToast } = useToast();

  const handleSubmit = async () => {
    // ...some async code

    triggerToast({
      type: "success",
      message: "Form updated successfully",
      duration: 5000,
    });
  };
};

You can see usage example in Form component, which is shown when you run the application in this repo

To see the example, run npm i and npm start from the terminal or use the codesandbox to see it immidiately in your browser.

Internals

State is stored in context ToasterContext, which is abstracted away with toastReducer via custom hooks: useToast and useToastInternal.

useToast is for usage inside the application logic, as explained above in the usage example.

useToastInternal, however provides a list, notifications and a closeToast which are used inside the Toasts component, where the React Portal is created to host our toasts. These two drive the toasts rendering and closing logic. Lastly, there is Toast component, which is the UI implementation of a toast notification.

Toast and it's parent, Toasts are styled with styled-components library and their styles live in the same folder with styles.js name.

Example App Screenshot

react-toast-notification's People

Contributors

adokce avatar

Watchers

 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.