• Stars
    star
    937
  • Rank 48,766 (Top 1.0 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Redux middleware that detects mutations between and outside redux dispatches. For development use only.

redux-immutable-state-invariant

Build Status Coverage Status

Redux middleware that spits an error on you when you try to mutate your state either inside a dispatch or between dispatches. For development use only!

Why?

Because you're not allowed to mutate your state in your reducers! And by extension, you shouldn't mutate them either outside. In order to change state in your app, you should always return a new instance of your state with the changes.

If you're using a library such as Immutable.js, this is automatically done for you since the structures provided by that library don't allow you to mutate them (as long as you don't have mutable stuff as values in those collections). However, if you're using regular objects and arrays, you should be careful to avoid mutations.

How to install

This lib is intended to use only during development. Don't use this in production!

npm install --save-dev redux-immutable-state-invariant

How to use

As said above, don't use this in production! It involves a lot of object copying and will degrade your app's performance. This is intended to be a tool to aid you in development and help you catch bugs.

To use it, just add it as a middleware in your redux store:

const {applyMiddleware, combineReducers, createStore} = require('redux');
const thunk = require('redux-thunk');
const reducer = require('./reducers/index');

// Be sure to ONLY add this middleware in development!
const middleware = process.env.NODE_ENV !== 'production' ?
  [require('redux-immutable-state-invariant').default(), thunk] :
  [thunk];

// Note passing middleware as the last argument to createStore requires redux@>=3.1.0
const store = createStore(
  reducer,
  applyMiddleware(...middleware)
);

Then if you're doing things correctly, you should see nothing different. But if you don't, that is, if you're mutating your data somewhere in your app either in a dispatch or between dispatches, an error will be thrown with a (hopefully) descriptive message.

API

immutableStateInvariantMiddleware({ isImmutable, ignore })

The default export is a factory to create the middleware. Supports an options argument (optional) to customize middleware behavior. It returns the middleware function when called. The following properties are supported in options:

Options

  • isImmutable function(value) - Specify if a value should be treated as immutable or not. The default implementation will return true for primitive types (like numbers, strings, booleans, null and undefined). Useful if some state of your app uses a library like Immutable.js and you want to let the middleware know that the data structures are indeed immutable.

  • ignore string[] - specify branch(es) of the state object to ignore when detecting for mutations. The elements of the array should be dot-separated "path" strings that match named nodes from the root state.

    // example: ignore mutation detection along the 'foo' & 'bar.thingsToIgnore' branches
    const middleware = immutableStateInvariantMiddleware({
      ignore: [
        'foo',
        'bar.thingsToIgnore'
      ]
    });

More Repositories

1

react-sound

Sound component to play audio in your web apps
JavaScript
485
star
2

graphql-tag.macro

Babel Macro for graphql-tag
JavaScript
167
star
3

activerecord-futures

Specify queries to be sent all at once to the database. Save round trips!
Ruby
107
star
4

graphql-persisted-document-loader

Webpack loader that adds a documentId to a compiled graphql document, which can be used when persisting/retrieving documents
JavaScript
81
star
5

workshop-pensando-en-react

Workshop introductorio a React
JavaScript
51
star
6

makery.js

Factory-style (via blueprints) object creation for tests. Inspired by Ruby Machinist.
JavaScript
22
star
7

fnky

A funky (and "souly"?) library for functional programming in Javascript
JavaScript
17
star
8

react-style-hot-reload

Exploring how to make react-style work with HMR and react-hot-loader
JavaScript
13
star
9

redux-state-router

Define your urls as a function of your state
JavaScript
8
star
10

cycle-poc

Playing with Cyclejs
JavaScript
5
star
11

StateMachine

State Machine pattern for .NET (inspired by ruby gem state_machine)
C#
4
star
12

rpg

This wanted be a super cool MMORPG, but I abandoned it :(
CoffeeScript
2
star
13

js-runner-fatigue

JS (runner) Fatigue - A game to cope with learning different JS stacks
JavaScript
2
star
14

graphql-client

wip
JavaScript
2
star
15

react-job-board

Quick example job board app using React.
JavaScript
2
star
16

enter-hangman

Hangman game made with React for a live coding session at Meetup.js Buenos Aires
JavaScript
2
star
17

demo_app

Some other app for ruby
Ruby
1
star
18

leoasis.github.com

My Personal Blog
JavaScript
1
star
19

stillness

Immutable manipulation helpers to aid in nested state updates
JavaScript
1
star
20

immutable-model

Immutable Model and Collection abstractions backed by Immutable.js
JavaScript
1
star
21

perf-graphql-tag

Repo to see how much time is saved when preprocessing graphql-tag calls
JavaScript
1
star
22

vows-test

Testing of the Vows module for node.js
JavaScript
1
star