Package Exports
- immutability-helper
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (immutability-helper) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
immutability-helper
Mutate a copy of data without changing the original source
This is a drop in replacement for react-addons-update
:
// import update from 'react-addons-update';
import update from 'immutability-helper';
const state1 = ['x'];
const state2 = update(state1, {$push: ['y']}); // ['x', 'y']
The main difference this has with react-addons-update
is that
you can extend this to give it more functionality:
update.extend('$addtax', function(tax, original) {
return original + (tax * original);
});
const state = { price: 123 };
const withTax = update(state, {
price: {$addtax: 0.8},
});
assert(JSON.stringify(withTax) === JSON.stringify({ price: 221.4 });
Note that original
in the function above is the original object, so if you plan making a
mutation, you must first shallow clone the object. Another option is to
use update
to make the change return update(original, { foo: {$set: 'bar'} })
If you don't want to mess around with the globally exported update
function you can make a copy and work with that copy:
import { newContext } from 'immutability-helper';
const myUpdate = newContext();
myUpdate.extend('$foo', function(value, original) {
return 'foo!';
});
Why?
react-addons-update
has react as a peer dependency. Also Facebook is deprecating react-addons-update
😦