Pre-reducer for UI reducers that can be loading or have an error
$ npm install --save redux-load-errorCompose your reducer with this pre-reducer to have it handle the loading and error properties for the UI component you are referencing.
You need to declare which actions will cause the component to load, display an error, or display its default content.
import * as actions from 'actions';
import loadAndError from 'redux-load-error';
const loadingActions = [actions.LOADING];
const notLoadingActions = [actions.DONE];
const errorActions = [actions.ERROR];
const reducer = (state = { foo: 'foo' }, action) => {
switch (action.type) {
case actions.FOO:
return { ...state, foo: action.foo };
default:
return state;
}
}
export default loadAndError(reducer, loadingActions, notLoadingActions, errorActions);Now the pre-reducer will handle the loading and error properties for you.
import { createStore } from 'redux';
import * as actions from 'actions';
import reducer from 'reducer';
let store = createStore(reducer);
store.dispatch({ type: actions.LOADING });
// loading: true, error: undefined
store.dispatch({ type: actions.DONE });
// loading: false, error: undefined
store.dispatch({ type: actions.ERROR, error: 'An error' });
// loading: false, error: 'An error'The base reducer to compose
An array of action types that will set loading to true and error to undefined
An array of action types that will set loading to false and error to undefined
An array of action types that will set loading to false and error to action.error
An object containing one, all or none of the following options:
A string that denotes the property key to use for loading, defaults to loading
A string that denotes the property key to use for error, defaults to error
If set to true nothing will set the error property to undefined, you can do it in your base reducer. Defaults to false.
- react-hoc-loading: HOC to show and hide a loading image or message across all your mayor UI components
- react-bootstrap-hoc-error: HOC to show and hide errors across all your mayor UI components using react-bootstrap
MIT © Marco Scabbiolo