Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-redux-loading-bar

mironov94.7kMIT5.0.8TypeScript support: included

Simple Loading Bar for Redux and React

react, redux, loading, loading-bar, progress, progress-bar, react-component

readme

React Redux Loading Bar

npm version build status coverage status npm downloads

A React component that provides Loading Bar (aka Progress Bar) for long running tasks.

Demo GIF

Consists of:

  • React component — displays loading bar and simulates progress
  • Redux reducer — manages loading bar's part of the store
  • (optional) Redux middleware — automatically shows and hides Loading Bar 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. You can attach it to the top of any block or the whole page.

You can even include multiple loading bars on the same page, that will render independently. They need to be provided with a scope so that you can adjust them independently.

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

export default class Header extends React.Component {
  render() {
    return (
      <header>
        <LoadingBar />
      </header>
      <section>
        <LoadingBar scope="sectionBar" />
      </section>
    )
  }
}

Install the reducer to the store:

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

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

Usage with redux-promise-middleware

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,
  // promise middleware
  applyMiddleware(loadingBarMiddleware())
)

Usage with custom suffixes or alternative promise middleware

You can configure promise type suffixes that are used in your project:

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

const store = createStore(
  rootReducer,
  applyMiddleware(
    loadingBarMiddleware({
      promiseTypeSuffixes: ['REQUEST', 'SUCCESS', 'FAILURE'],
    })
  )
)

Usage with custom scope (for multiple loading bars)

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

const store = createStore(
  rootReducer,
  applyMiddleware(
    loadingBarMiddleware({
      scope: 'sectionBar',
    })
  )
)

If you're not using redux-promise-middleware or any other 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 without middleware but with scope

You need to provide the scope to the actions:

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

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

Usage with redux-saga

Install the loadingBarReducer() and mount Loading Bar in your application. You can import and dispatch showLoading and hideLoading from your sagas.

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

export function* fetchData() {
  try {
    yield put(showLoading())
    const payload = yield call(API, params)
    // payload processing
  } finally {
    yield put(hideLoading())
  }
}

Usage with immutable-js

You can change component import line if your top level redux store object is immutable.

import { ImmutableLoadingBar as LoadingBar } from 'react-redux-loading-bar'

// Mount LoadingBar component as usual

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.

RTL (Right-To-Left) Layout

Pass direction="rtl" to make Loading Bar simulate progress from right to left:

<LoadingBar direction="rtl" />

Styling

You can apply custom styling right on the LoadingBar component. For example you can change the color and height of the loading bar:

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

Alternatively, you can specify your own CSS class.

Please note that will disable default styling (which is background-color: red; height: 3px; position: absolute;).

<LoadingBar className="loading" />

Don't forget to set height, background-color and position for the loading class in your CSS files.

Configure Progress Simulation

You can change updateTime (by default 200ms), maxProgress (by default 90%) and progressIncrease (by default 5%):

<LoadingBar updateTime={100} maxProgress={95} progressIncrease={10} />

By default, the Loading Bar will only display if the action took longer than updateTime to finish. This helps keep things feeling snappy, and avoids the annoyingness of showing a Loading Bar for fractions of seconds. If you want to show Loading Bar even on quickly finished actions you can pass the showFastActions prop:

<LoadingBar showFastActions />

Reset progress

You can dispatch the resetLoading action to ultimately hide Loading Bar even when multiple long running tasks are still in progress.

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.

Contributors (in chronological order)

To see what has changed in recent versions of Loading Bar, see the CHANGELOG.

Licensed MIT. Copyright 2016-current Anton Mironov.

changelog

Release History

5.0.8

  • Update types definition (thanks @MasterOdin)

5.0.7

  • Publish latest build

5.0.6

  • Re-export constants from reducer (thanks @MasterOdin)

5.0.5

  • Include latest redux as peer dependency

5.0.4

  • Update dependencies

5.0.3

  • Support latest react 18 and react-redux v8

5.0.2

  • Bump dependencies and fix merge conflict

5.0.1

  • Bump dependencies to support React 17

5.0.0

  • Change the animation to use width instead of transform (thanks @exzizt)

4.6.0

  • Update dev dependencies and rebuild with newer Babel

4.5.0

  • Cancel termination animation if Loading Bar should be restarted

4.4.0

  • Update react-redux peer dependency requirement (contributed by @hugomn)

4.3.0

  • Add support for RTL Layout. Ref: #88

4.2.0

  • Support latest react-redux v6

4.1.0

  • Make the animation smoother (at least in Chrome)

4.0.8

  • Old browser compatibility (contributed by @dengbupapapa)

4.0.7

  • Revert changes introduced in v4.0.6 because they break modern browsers like Chrome

4.0.6

  • Animation needs to be compatible with ie9 and other browsers (contributed by @dengbupapapa)

4.0.5

  • Replace .includes with a broadly supported .indexOf alternative

4.0.4

  • Relax Redux dependency

4.0.3

  • Update to latest react-lifecycles-compat

4.0.2

  • Rebuild Loading Bar

4.0.1

  • Update react-lifecycles-compat dependency

4.0.0

  • Rewrite the code to be compatible with future React versions

3.1.2

  • Check for the scope property within the meta property of the action

3.1.1

  • Add scope support to type definitions (contributed by @Kovensky)

3.1.0

  • Fix not disappearing loading bar on immediately changed props (contributed by @MikeDevice)

3.0.3

  • No need to have the immutableLoadingBarReducer

3.0.2

  • Import immutable in immutable reducer

3.0.1

  • Add immutable reducer to make Loading Bar v3 work with immutable stores

3.0.0

  • Allow having multiple loading bars on one page (contributed by @seb0zz and @neogermi)

2.9.3

  • Bump dependencies to support React 16

2.9.2

  • Make terminating animation faster. Ref: #41

2.9.1

  • Render an empty div first and replace it with the actual Loading Bar after mount. This fixes the problem with SSR and strict style CSP. Ref: #39

2.9.0

  • Make the animation smoother

2.8.2

  • Add showFastActions prop to TS definition (thanks @vitosamson)

2.8.1

  • React.propTypes -> PropTypes

2.8.0

  • Do not display Loading Bar for quickly finished actions. You can pass the showFastActions prop to show the Loading Bar even when the action finishes in under updateTime.

2.7.4

  • Do not set second interval if loading bar is shown

2.7.3

  • Fix race condition when showLoading is called right after hideLoading

2.7.2

  • Do not try to stop simulation if it hasn't begun

2.7.1

  • Launch progress simulation only once when loading is increased couple of times

2.7.0

  • Revamped animation: added slowdown to the end of the progress; instant actions will briefly display loading bar for the period of UPDATE_TIME * 2

2.6.6

  • Fix issue where setState() is called on the server-side at unexpected lifecycles

2.6.5

  • Export resetLoading action

2.6.4

  • Do not let percent become greater than maxProgress if progressIncrease > (100 - maxProgress)

2.6.3

  • TypeScript definitions are not required to make LoadingBar work and thus removed from peer dependencies (thanks @larrydahooster)

2.6.2

  • Add TypeScript definitions (thanks @janslow)

2.6.1

  • Bump react-redux dependency version (thanks @larrydahooster)

2.6.0

  • New action resetLoading to reset the loading counter and hide Loading Bar.

2.5.0

  • Ability to use loading bar with immutable (thanks @greenpart)

2.4.1

  • If the Loading Bar is mounted with loading count > 0, it should launch the progress simulation immediately

2.4.0

  • Do not apply styling if CSS class is specified

2.3.4

  • Export UI component

2.3.3

  • Reset position of the Loading Bar if it is triggered to be shown during ending animation

2.3.2

  • Fix infinite loop when Loading Bar is triggered to be shown during ending animation

2.3.1

  • Do not call resetProgress after the Loading Bar is unmounted

2.3.0

  • Simplify the middleware

2.2.3

  • Loading Bar should not reset to 0 before disappear

2.2.2

  • Add fade effect on SHOW and HIDE actions by using opacity transition (thanks to @hieuhlc)

2.2.1

  • Match actions with regular expressions to prevent false positives (thanks to @ThomasMarnet)

2.2.0

  • Loading Bar moves to the end before disappear

2.1.0

  • Configure updateTime, maxProgress and progressIncrease via props

2.0.2

  • Clear interval on unmount

2.0.1

  • Fix for server side rendering and isomorphic apps

2.0.0

  • Ability to set custom promise type suffixes

1.1.1

  • Remove shrinkwrap to make the module portable

1.1.0

  • Add ability to apply custom styling and relax dependencies

1.0.2

  • Fix middleware to work with redux-thunk

1.0.1

  • Update dependencies

1.0.0

  • Initial release