Coder Social home page Coder Social logo

react-redux-alerts's Introduction

#react-redux-alerts

React Redux Alerts is redux based alert system designed for creating resuable alerts across your application. It consists of two things: a redux reducer and a React higher order component to keep track of when your alert is to render.

Installation

Simply run npm i react-redux-alerts --save to add to your project.

Getting started with react-redux-alerts

###Step 1 The first thing to do is mount the 'react-redux-alerts' reducer to Redux. This only has to be done at startup. Make sure it's mounted to the alerts key.

import { createStore, combineReducers } from 'redux';
import { reducer as alertsReducer } from 'react-redux-alerts';

const reducers = {
    alerts: alertsReducer
};

const reducer = conbineReducer(reducers);
const store = createStore(reducer);

###Step 2 Decoreate your alert component with createAlert(). This will provide your component with two props: message and close(). The first prop is passed in when the createAction is called and the second is a function that can be called from within your component to dimiss it.

// Alert.js

import React from 'react';
import { createAlert } from 'react-redux-alerts';

const MyAlert = ({message, close}) => (
    <div className='myAlert'>
        {message}
        <button onClick={close}> Click to Dismiss me </button>
    </div>
);

/** 
 * This is a wrapper method that connects your alert to the store based on a *alertName key. This is the unique identifier that will allow you to both show the alert and dismiss the alert. 
 */
export default createAlert({
    alertName: 'myAlert'
})(MyAlert);

This wrapped component can then be pluged into any container that you want the alert to show up in. To show the notification, simply dispatch the createAlert action with two arguments: the alertName and the alertMessage. To close, simply call the dismissAlert action with the correct key.

// MyContainer.js

import React, { Component, PropTypes } from 'react';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { actions as alertActions } from 'react-redux-alerts';
import MyAlert from './Alert';

class MyContainer extends Component {
    render() {
        return(
            <div>
                This is my custom container.
                <MyAlert />
                <button onClick={() => this.props.createAlert('myAlert', 'My Alert Message')}
                    Create Alert!
                </button>
                <button onClick={() => this.props.dismissAlert('myAlert')}
                    Dismiss Alert!
                </button>
            </div>
        )
    }
}

function mapDispatchToProps(dispatch) {
  return { alerts: bindActionCreators(alertActions, dispatch) };
}

export default connect(mapStateToProps, mapDispatchToProps)(MyContainer);

react-redux-alerts's People

Contributors

jmenestr avatar cable729 avatar

Watchers

James Cloos 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.