GithubHelp home page GithubHelp logo

ptzagk / redux-ignore Goto Github PK

View Code? Open in Web Editor NEW

This project forked from omnidan/redux-ignore

0.0 0.0 0.0 24 KB

:recycle: higher-order reducer to ignore redux actions

License: MIT License

JavaScript 100.00%

redux-ignore's Introduction

redux-ignore

NPM version (>=1.2) Build Status Dependencies js-standard-style https://paypal.me/DanielBugl/10 https://gratipay.com/~omnidan/

higher-order reducer to ignore redux actions

can be used to avoid performance problems in large apps by skipping reducer subtrees

Installation

npm install --save redux-ignore

API

import { ignoreActions, filterActions } from 'redux-ignore';

ignoreActions(reducer, [ARRAY_OF_ACTIONS])
ignoreActions(reducer, (action) => !action.valid)

filterActions(reducer, [ARRAY_OF_ACTIONS])
filterActions(reducer, (action) => action.valid)

Ignoring Actions

redux-ignore is a reducer enhancer (higher-order reducer), it provides the ignoreActions function, which takes an existing reducer and either:

  • An array of actions to be ignored, or...
  • A predicate function for filtering out actions.

Firstly, import redux-ignore:

// Redux utility functions
import { combineReducers } from 'redux';
// redux-ignore higher-order reducer
import { ignoreActions } from 'redux-ignore';

Then, add ignoreActions to your reducer(s) like this:

combineReducers({
  counter: ignoreActions(counter, [INCREMENT_COUNTER])
});

Now you won't be able to increment the counter anymore, because the INCREMENT_COUNTER action is ignored.

Alternatively, you can ignore actions via a predicate function:

combineReducers({
  counter: ignoreActions(counter, (action) => action.type === INCREMENT_COUNTER)
});

Filtering Actions

You can also use filterActions to only accept actions that are declared in an array, or that satisfy the predicate function:

import { combineReducers } from 'redux';
import { filterActions } from 'redux-ignore'; // pull in the filterActions function
import { STAY_COOL, KEEP_CHILLIN } from './actions';
import { counter, notACounter } from './reducers';

combineReducers({
  counter: filterActions(counter, (action) => action.type.match(/COUNTER$/)), // only run on actions that satisfy the regex
  notACounter: filterActions(notACounter, [STAY_COOL, KEEP_CHILLIN]) // only run for these specific relaxing actions
});

What is this magic? How does it work?

Have a read of the Implementing Undo History recipe in the Redux documents, which explains in detail how higher-order reducers work.

License

MIT, see LICENSE.md for more information.

redux-ignore's People

Contributors

davidkpiano avatar greenkeeperio-bot avatar juanpicado avatar maxmechanic avatar omnidan 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.