Legacy React addon providing immutable update operations for state management without deep copying
npx @tessl/cli install tessl/npm-react-addons-update@15.6.0React Addons Update is a legacy React addon that provides immutable update operations for JavaScript objects and arrays. It enables efficient state management without mutating original data structures or performing expensive deep copies.
npm install react-addons-updateimport update from 'react-addons-update';For CommonJS:
const update = require('react-addons-update');For browser usage with React already loaded:
<!-- development version -->
<script src="https://unpkg.com/react-addons-update/react-addons-update.js"></script>
<!-- Then use React.addons.update -->import update from 'react-addons-update';
// Simple array update
const initialArray = [1, 2, 3];
const newArray = update(initialArray, {$push: [4]});
// Result: [1, 2, 3, 4], initialArray unchanged
// Object property update
const obj = {a: 5, b: 3};
const newObj = update(obj, {b: {$set: 6}});
// Result: {a: 5, b: 6}, obj unchanged
// Nested object update
const state = {
user: {name: 'Alice', preferences: {theme: 'dark'}},
posts: [1, 2, 3]
};
const newState = update(state, {
user: {preferences: {theme: {$set: 'light'}}},
posts: {$push: [4]}
});React Addons Update uses a command-based approach to immutable updates:
shallowCopy() function$push, $unshift, $splice, $set, $merge, $apply) handle different update patternsThe library ensures data integrity through extensive invariant checking and type validation.
The main function that returns an updated shallow copy of an object or array without mutating the original.
/**
* Returns an updated shallow copy of an object without mutating the original
* @param {any} value - The target object/array to update
* @param {object} spec - Update specification containing commands
* @returns {any} Updated shallow copy of the input value
* @throws {Error} Invariant errors for invalid specifications
*/
function update(value, spec);The spec parameter is an object that describes how to update the value. It uses special command keys prefixed with $ to specify different types of updates.
Usage Examples:
// Basic object update
const person = {name: 'John', age: 30};
const updatedPerson = update(person, {age: {$set: 31}});
// Array manipulation
const numbers = [1, 2, 3];
const moreNumbers = update(numbers, {$push: [4, 5]});
// Complex nested update
const data = {
users: [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}],
settings: {theme: 'dark', notifications: true}
};
const updatedData = update(data, {
users: {0: {name: {$set: 'Alice Smith'}}},
settings: {$merge: {language: 'en'}}
});Adds items to the end of an array target.
// Command syntax: {$push: array}
// Pushes all items in the array to the target arrayUsage Example:
const fruits = ['apple', 'banana'];
const moreFruits = update(fruits, {$push: ['orange', 'grape']});
// Result: ['apple', 'banana', 'orange', 'grape']Adds items to the beginning of an array target.
// Command syntax: {$unshift: array}
// Unshifts all items in the array to the target arrayUsage Example:
const numbers = [3, 4, 5];
const moreNumbers = update(numbers, {$unshift: [1, 2]});
// Result: [1, 2, 3, 4, 5]Performs splice operations on an array target using standard Array.splice syntax.
// Command syntax: {$splice: array_of_arrays}
// Each inner array contains splice arguments: [start, deleteCount, ...items]Usage Example:
const letters = ['a', 'b', 'c', 'd'];
const modified = update(letters, {
$splice: [[1, 2, 'x', 'y']] // Remove 2 items at index 1, insert 'x', 'y'
});
// Result: ['a', 'x', 'y', 'd']
// Multiple splice operations
const data = [1, 2, 3, 4, 5];
const result = update(data, {
$splice: [
[0, 1], // Remove first item
[2, 0, 'inserted'] // Insert at index 2
]
});Replaces the target entirely with the provided value.
// Command syntax: {$set: any}
// Replaces the target entirely with the provided valueUsage Example:
const config = {api: 'v1', timeout: 5000};
const newConfig = update(config, {api: {$set: 'v2'}});
// Result: {api: 'v2', timeout: 5000}
// Can set any type
const data = {items: [1, 2, 3]};
const cleared = update(data, {items: {$set: []}});
// Result: {items: []}Performs a shallow merge of the provided object with the target object.
// Command syntax: {$merge: object}
// Merges the keys of the object with the target (shallow merge)Usage Example:
const user = {name: 'Alice', age: 25};
const updated = update(user, {$merge: {age: 26, city: 'Boston'}});
// Result: {name: 'Alice', age: 26, city: 'Boston'}
// Merge with nested objects
const settings = {ui: {theme: 'dark'}, api: {version: 1}};
const newSettings = update(settings, {
ui: {$merge: {language: 'en'}},
api: {$merge: {timeout: 3000}}
});Applies a function to the current value and updates with the returned result.
// Command syntax: {$apply: function}
// Passes current value to function and updates with returned valueUsage Example:
const counter = {count: 5};
const incremented = update(counter, {
count: {$apply: (x) => x + 1}
});
// Result: {count: 6}
// Transform array items
const items = {data: [1, 2, 3]};
const doubled = update(items, {
data: {$apply: (arr) => arr.map(x => x * 2)}
});
// Result: {data: [2, 4, 6]}
// Complex transformations
const text = {content: ' hello world '};
const cleaned = update(text, {
content: {$apply: (str) => str.trim().toUpperCase()}
});
// Result: {content: 'HELLO WORLD'}When using $set, it must be the only key in the specification object:
// Valid
update(obj, {field: {$set: 'new value'}});
// Invalid - will throw error
update(obj, {field: {$set: 'new value', $merge: {other: 'data'}}});Array commands ($push, $unshift, $splice) require array targets:
// Valid
update([1, 2, 3], {$push: [4]});
// Invalid - will throw error
update({a: 1}, {$push: [2]});All commands validate their inputs and targets:
// $apply requires a function
update(obj, {field: {$apply: 'not a function'}}); // Throws error
// $merge requires an object
update(obj, {$merge: 'not an object'}); // Throws error
// Array commands require array values
update(arr, {$push: 'not an array'}); // Throws errorThe update function throws invariant errors for:
$set command$apply)Example error handling:
try {
const result = update(data, {$invalidCommand: 'value'});
} catch (error) {
console.error('Update specification invalid:', error.message);
}This package is deprecated and no longer maintained. For new projects, use immutability-helper which provides the same API as a drop-in replacement.
// Migration
// Old: import update from 'react-addons-update';
// New: import update from 'immutability-helper';