意识到当你为你的树结构使用 Immutable 时,你不能期望在不更改对该节点的内部引用的情况下替换一个节点,这意味着这样的更改将需要冒泡到树的根,同时也会更改根.
更详细:一旦您使用方法更改某个属性值,您将获得为该特定节点返回的新对象。然后在你的树中重新注入那个新节点,即在父级的子级列表中,你将使用一个方法来创建一个新的子级列表(因为children属性也是不可变的)。现在的问题是将子列表附加到父节点,这将产生一个新的父节点,......等等。您最终将重新创建要更改的节点的所有祖先节点,为您提供一个新的树实例,该实例将重用不在根到节点路径中的节点。
要重新使用您的图像,您将获得如下信息:
Immutable API 可以使用该updateIn
方法为您执行此操作(或者setIn
如果您的更新仅涉及目标节点的一个属性)。您需要将关键路径传递给它以识别要修改的(嵌套)节点。
因此,例如,如果您知道要更改的节点的id,则可以使用一个小辅助函数来查找到该特定节点的关键路径。
function findKeyPathOf(tree, childrenKey, predicate) {
var path;
if (Immutable.List.isList(tree)) {
tree.some(function (child, i) {
path = findKeyPathOf(child, childrenKey, predicate);
if (path) return path.unshift(i); // always returns truthy
});
return path;
}
if (predicate(tree)) return [];
path = findKeyPathOf(tree.get(childrenKey), childrenKey, predicate);
if (path) return [childrenKey].concat(path);
}
您需要将树、具有子项的属性的名称(children
在您的情况下)以及用于识别您要查找的节点的函数传递给它。假设您想要 id 为 4 的节点的路径,那么您可以这样称呼它:
var keyPath = findKeyPathOf(tree, 'children', node => node.get('id') == 4);
该关键路径可能看起来像这样 - 数组中索引的更改,以及children
提供更深数组的属性:
[0, 'children', 0, 'children', 1]
然后要修改该路径上的节点,您可以执行以下操作:
var newTree = tree.updateIn(keyPath, node => node.set('name', 'Hello'));
这是一个带有一些示例数据的演示:
// Function to get the path to a certain node in the tree
function findKeyPathOf(tree, childrenKey, predicate) {
var path;
if (Immutable.List.isList(tree))
childrenKey = tree.findKey(child =>
path = findKeyPathOf(child, childrenKey, predicate));
else if (predicate(tree))
return [];
else
path = findKeyPathOf(tree.get(childrenKey), childrenKey, predicate);
return path && [childrenKey].concat(path);
}
// Function to compare two trees
function differences(tree1, tree2, childrenKey) {
if (Immutable.List.isList(tree1)) {
return tree1.reduce(function (diffs, child, i) {
return diffs.concat(differences(child, tree2.get(i), childrenKey));
}, []);
}
return (tree1 !== tree2 ? [tree1] : [])
.concat(differences(tree1.get(childrenKey), tree2.get(childrenKey),
childrenKey));
}
// Sample data
var tree = [{
id: 1,
name: 'Mike',
children: [{
id: 2,
name: 'Helen',
children: [{
id: 3,
name: 'John',
children: []
},{
id: 4,
name: 'Sarah',
children: [{
id: 5,
name: 'Joy',
children: []
}]
}]
}]
}, {
id: 6,
name: 'Jack',
children: [{
id: 7,
name: 'Irene',
children: []
},{
id: 8,
name: 'Peter',
children: []
}]
}];
// Create immutable tree from above plain object:
var tree = Immutable.fromJS(tree);
// Use the function to find the node with id == 4:
var keyPath = findKeyPathOf(tree, 'children', node => node.get('id') == 4);
// Found it?
if (keyPath) {
// Set 'name' to 'Hello' in that node:
var newTree = tree.updateIn(keyPath, node => node.set('name', 'Hello'));
// Print the new tree:
console.log(newTree.toJS());
// Compare all nodes to see which ones were altered:
var altered = differences(tree, newTree, 'children').map(x => x.get('id'));
console.log('IDs of nodes that were replaced: ', altered);
} else {
console.log('Not found!');
}
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.1/immutable.min.js"></script>