Replaces a node in a tree's given index path, and returns a new object. Reuses existing objects where possible.
Useful for updating recursive & immutable data structures in React or Redux.
const tree = [
{
name: 'parent',
children: [
{
name: 'child 1',
},
{
name: 'child 2',
},
],
},
{
name: 'another parent',
children: null,
},
];
const newTree = updateIn(tree, [0, 1], { name: 'child 2a' });
// newTree shape:
// [
// {
// name: 'parent',
// children: [
// {
// name: 'child 1',
// },
// {
// name: 'child 2a',
// },
// ],
// },
// {
// name: 'another parent',
// children: null,
// },
// ]
// equality:
// newTree !== tree
// newTree[0] !== tree[0]
// newTree[0].children[0] === tree[0].children[0]
// newTree[0].children[1] !== tree[0].children[1]
// newTree[1] === tree[1]
const newTree = updateIn(tree, [0, 1], null);
- pass
null
as the replacement