Coder Social home page Coder Social logo

dushyantsingh42 / react-redux-loading-bar Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mironov/react-redux-loading-bar

0.0 1.0 0.0 958 KB

Simple Loading Bar for Redux and React

Home Page: http://mironov.github.io/react-redux-loading-bar/

License: MIT License

JavaScript 100.00%

react-redux-loading-bar's Introduction

React Redux Loading Bar

npm version build status coverage status npm downloads dependency status

A simple React component that provides Loading Bar (aka Progress Bar) for long running tasks. Works out of the box with redux-promise-middleware and can be easily tweaked for other usage.

Consists of:

  • React component — displays loading bar and simulates progress
  • Redux reducer — manages loading bar's part of the store
  • Redux middleware — dispatches SHOW/HIDE for actions with promises

Examples

See Demo or its source code.

Installation

npm install --save react-redux-loading-bar

Usage

Mount the LoadingBar component anywhere in your application:

import LoadingBar from 'react-redux-loading-bar'

export default class Header extends React.Component {
  render() {
    return (
      <header>
        <LoadingBar />
      </header>
    )
  }
}

Good news is that it doesn't include any positioning, so you can attach it to the top of any block or the whole page.

Install the reducer to the store:

import { combineReducers } from 'redux'
import { loadingBarReducer } from 'react-redux-loading-bar'

const reducer = combineReducers({
  // app reducers
  loading: loadingBarReducer,
})

(Optional) Apply middleware to automatically show and hide loading bar on actions with promises:

import { createStore, applyMiddleware } from 'redux'
import { loadingBarMiddleware } from 'react-redux-loading-bar'
import rootReducer from './reducers'

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

If you're not using redux-promise-middleware, you can skip installing the loadingBarMiddleware and dispatch SHOW/HIDE actions manually. The other option is to write your own middleware that will be similar to the bundled one.

Usage without middleware

You can dispatch SHOW/HIDE actions wherever you want by importing the corresponding action creators:

import { showLoading, hideLoading } from 'react-redux-loading-bar'

dispatch(showLoading())
// do long running stuff
dispatch(hideLoading())

You need to dispatch HIDE as many times as SHOW was dispatched to make the bar disappear. In other words, the loading bar is shown until all long running tasks complete.

Usage with jQuery Ajax Requests

If you happen to use jQuery for Ajax requests, you can dispatch SHOW/HIDE actions on ajaxStart/ajaxStop global events:

$(document).on('ajaxStart', this.props.actions.showLoading)
$(document).on('ajaxStop', this.props.actions.hideLoading)

See a demo or checkout the code.

Styling

You can apply custom styling right on the LoadingBar component, for example you can change the color and height of it:

<LoadingBar style={{ backgroundColor: 'blue', height: '5px' }} />

Or specify your own CSS class:

<LoadingBar className="loading" />

Tests

npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Release History

  • 1.0.0 Initial release
  • 1.0.1 Update dependencies
  • 1.0.2 Fix middleware to work with redux-thunk
  • 1.1.0 Add ability to apply custom styling and relax dependencies
  • 1.1.1 Remove shrinkwrap to make the module portable

Licensed MIT. Copyright 2016-current Anton Mironov.

react-redux-loading-bar's People

Contributors

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