Coder Social home page Coder Social logo

alexdmejias / react-redux-notification Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nicmesan/react-redux-notification

0.0 2.0 0.0 16 KB

The simplest way to implement a notification system in your React-Redux app

License: MIT License

JavaScript 83.78% CSS 16.22%

react-redux-notification's Introduction

react-redux-notification

The simplest way to implement a notification system in your React-Redux app

Installation

npm install --save react-redux-notification

Getting started

CSS

Webpack:

import 'react-redux-notification/src/styles/notifications.css';

Other

<link rel="stylesheet" type="text/css" href="path/to/notifications.css">

Middleware

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import App from './my_app';
import { createStore, applyMiddleware } from 'redux';
import { Provider } from 'react-redux';
import {notificationsMiddleware} from 'react-redux-notification'
import rootReducer from './my_reducer';

const store = createStore(
    rootReducer,
	applyMiddleware(notificationsMiddleware),

	);

ReactDOM.render(
	<Provider store={store}>
		<App />
	</Provider>
	, document.querySelector('.container'));

Reducer

import { combineReducers } from 'redux';
import { notificationsReducer } from '../src'

const rootReducer = combineReducers({
    notificationsReducer,
});

export default rootReducer;

Using the library

Notifications container

First, add the notifications container in your main component:

import React, { Component } from 'react';
import { Notifications } from 'react-redux-notification'

class MainComponent extends Component {
  render () {
    return (
      <div>
        //.....
        <Notifications duration={1000}/>
      </div>
    )
}
//...

Notification container Props

Name Type Default Required
duration number 3000 false
  • duration: Time in ms each single notification will be rendered before disappearing

Actions

Adding a single notification

You can add a notification calling the addNotification action:

import React, { Component } from 'react';
import { Notifications } from 'react-redux-notification';
import { addNotification } from 'react-redux-notification/actions';

class MainComponent extends Component {
  
  addNotification () {
    const notificationPayload = {
            text: 'A notification Message',
        };
        
    this.props.addNotification(notificationPayload)
 }
 
  render () {
    return (
      <div>
        <button onClick={this.addNotification.bind(this)}
        //.....
        <Notifications duration={1000}/>
      </div>
    )
}
//...

Or just add the notifiaction key to any other action you are dispatching, and the library middleware will take care of the rest:

// Some random action in your app

exports function doSomeStuff () {
  type: 'doSomeStuff',
  notification: {
    text: 'Stuff done'
}

Single notification Props

Name Type Default Required
message string None true
className string Default styles false
unique boolean false false
type string None false
  • message: Text message to be rendered.
  • className: Class name to be applied to this specific notification. The library comes with an error class to render error like notifications.
  • unique: If true, it will check if a notification of the same "type" exists and if it does, will replace it instead of stacking it.
  • type: Related to the unique props, group related notifications of the same type if you don't want them to stack.

Clearing all notifications

Call clearAllNotifications action to clear all current notifications:

import React, { Component } from 'react';
import { Notifications } from 'react-redux-notification';
import { clearAllNotifications } from 'react-redux-notification/actions';

class MainComponent extends Component {
  
  clearAllNotifications () {
    this.props.clearAllNotifications()
 }
 
  render () {
    return (
      <div>
        <button onClick={this.clearAllNotifications.bind(this)}
        //.....
        <Notifications duration={1000}/>
      </div>
    )
}
//...

Demo

Check the demo folder for an example

react-redux-notification's People

Contributors

nicmesan avatar mathiasms avatar

Watchers

James Cloos avatar Alex Mejias 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.