GithubHelp home page GithubHelp logo

jony89 / reducer-action-interceptor Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 0.0 139 KB

Intercept easily your action creators and reducers

License: MIT License

JavaScript 100.00%
redux redux-thunk redux-actions reducer

reducer-action-interceptor's Introduction

reducer-action-interceptor

These plugin allows you on one hand to intercept action creators in order to patch the action before bubbling up to the reducers, and on the other hand to intercept the reducers in order to take advantage of that interception.

For example, one can use this to create multiple instances of the same container within the same page, without one affecting the other.

React example

Let's say we want two instances of MyContainer.

MyContainer should export it's actionCreators object. Usually this object passed to the connect function where the mapDispatchToProps attribute comes in. These are the actions that we will intercept.

Now we can create two instances :

import { connect } from 'react-redux';
import { actionCreatorsInterceptor } from 'reducer-action-interceptor';
import MyContainer, { actionCreators } from '../MyContainer';

/**
 * First Container 
**/
export const MyFirstContainerConnected = connect(
  state => ({ ...state.firstContainerReducer }),
  actionCreatorsInterceptor(actionCreators, 'INSTANCE1'),
)(MyContainer);

/**
 * Second Container 
**/
export const MySecondContainerConnected = connect(
  state => ({ ...state.secondContainerReducer }),
  actionCreatorsInterceptor(actionCreators, 'INSTANCE2'),
)(MyContainer);

Obviously, we need to take care of the reducers as well. using the HOR - reducerInterceptor :

import { combineReducers } from 'redux';
import { reducerInterceptor } from 'reducer-action-interceptor';
import someGenericReducer from './someGenericReducer.reducer';

export default combineReducers({
  firstContainerReducer: reducerInterceptor(someGenericReducer, 'INSTANCE1'),
  secondContainerReducer: reducerInterceptor(someGenericReducer, 'INSTANCE2'),
});

And we are done, each action for each action is solely for that container.

The values INSTANCE1, INSTANCE2, can be anything you like, even a random number!

More over, the intercepted reducers can still accept non-intercepted actions, so no worries, you are still able to listen to other actions raised from all over the app.

reducer-action-interceptor's People

Contributors

jony89 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

reducer-action-interceptor's Issues

How to create dynamic reducer in combinereducers?

How to create dynamic reducer?

export default combineReducers({
firstContainerReducer: reducerInterceptor(someGenericReducer, 'INSTANCE1'),
secondContainerReducer: reducerInterceptor(someGenericReducer, 'INSTANCE2'),
});

I call API to get the category list and theirs products.

So I want to use multi instance of Product Components. But the issue is have to create multi action . Do your solution can resolve my problem?

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.