Giter Club home page Giter Club logo

react-alert-with-context's Introduction

Alert System in React

A reuseable alert system with React.

Run Locally

Clone the project

  git clone https://github.com/Riley1101/react-alert-with-context

Go to the project directory

  cd react-alert-with-context

Install dependencies

  yarn 

Start the server

  yarn add

Component Props

Alert component Props

CustomAlert.propTypes = {
    alert: PropTypes.shape({
        id: PropTypes.string.isRequired,
        content: PropTypes.shape({
            title: PropTypes.string.isRequired,
            message: PropTypes.string.isRequired,
            link: PropTypes.string
        }),
        duration: PropTypes.number,
        alerttype: PropTypes.oneOf(ALERT_TYPES),
    })
};

Alert container

AlertContainer.propTypes = {
    alerts: PropTypes.arrayOf(
        PropTypes.shape({
            id: PropTypes.string.isRequired,
            content: PropTypes.shape({
                title: PropTypes.string.isRequired,
                message: PropTypes.string.isRequired,
                link: PropTypes.string
            }),
            duration: PropTypes.number,
            alerttype: PropTypes.oneOf(ALERT_TYPES),
        })
    )
};

Setup

To use react-alert-with-context , wrap main app body with AlertProvider

import AlertProvider from '..../alert/provider';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <React.StrictMode>
      <AlertProvider>
        <App />
      </AlertProvider>
  </React.StrictMode>
);

To reuse the alert in your project

import { useAlert } from '..../alert';
let { dispatch } = useAlert();

Example

<Button
    variant='outlined'
    onClick={() =>
        dispatch({
            type: "CLEAR",
        })}>
    Close
</Button>

Dispatch actions

// to call an alert component
dispatch({
    type: "ADD",            // dispatch action types
    alerttype: 'success',   // success,info,warning,error
    duration: duration,     // duration on how long the alert to be shown default is 3000ms
    content: {
            title: title,
            message: message,
            link: link
        }
})
// to remove specific alert component
dispatch({
    type: "REMOVE",            // dispatch action types
    alert : 'id'               // id of the alert 
})
// to remove all alerts
dispatch({
    type: "CLEAR",            // dispatch action types

})

react-alert-with-context's People

Contributors

riley1101 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.