redux-on-action

1.0.0-rc3 • Public • Published

redux-on-action

Do something async when Redux action happens. Yet another (tiny) Redux async middleware.

Table of Contents

Install

npm install --save redux-on-action

Usage

First you have to apply the middleware:

import { createStore, applyMiddleware } from 'redux';
import createOnActionMiddleware from 'redux-on-action';
import rootReducer from './reducers/index';

const { middleware, onAction } = createOnActionMiddleware();

const store = createStore(
  rootReducer,
  applyMiddleware(middleware),
);

Then you can setup action listener:

onAction('IMPORT_START_REQUEST', async (action, dispatch, getState) => {
  dispatch({ type: 'IMPORT_STARTED' });

  await importDirectory(
    action.payload.directoryPath,
    importedFileCount => dispatch({ type: 'IMPORT_PROGRESS', payload: importedFileCount }),
  );

  dispatch({ type: 'IMPORT_FINISHED' });
});

API

createOnActionMiddleware

Returns object with fields:

onAction

Registers action listener. When specified action is dispatched - provided function will be called.

Arguments

  • action - String, type of Redux action.
  • callback - onActionCallback, function that will be called when action is dispatched.

onActionCallback

Function specified as onAction callback.

Arguments

Mulitple action listeners

You can setup many listeners for single action type. Every registered callback will be called in order in which they were registered.

onAction('MY_ACTION', () => console.log('first!'));
onAction('MY_ACTION', () => console.log('second!'));

dispatch({ type: 'MY_ACTION' });
// first!
// second!

License

This project is licensed under the MIT license.

Readme

Keywords

none

Package Sidebar

Install

npm i redux-on-action

Weekly Downloads

1

Version

1.0.0-rc3

License

MIT

Unpacked Size

8.9 kB

Total Files

12

Last publish

Collaborators

  • deseteral