3.4 KiB
Globals / "traverse" /
External module: "traverse"
Index
Interfaces
Functions
Functions
traverse
▸ traverse(node: FiberNode, fn: function, traverseConfig?: TTraverseConfig): void
Defined in traverse.ts:146
Traverse nodes recursively in depth-first manner, starting from a start node.
This is the default and basic traversal function, which covers basic use cases. You can't do advanced things like change the order of traversal, skip or cancel traversal after any node, etc. For more advanced usecases, see traverseGenerator
example
// calls fn for each node inside startNode
traverse(startNode, fn);
Parameters:
▪ node: FiberNode
▪ fn: function
▸ (node: FiberNode): any
Parameters:
| Name | Type |
|---|---|
node |
FiberNode |
▪Optional traverseConfig: TTraverseConfig
Returns: void
traverseGenerator
▸ traverseGenerator(node: FiberNode, __namedParameters: object): IterableIterator<FiberNode>
Defined in traverse.ts:69
Traverse nodes recursively using generators.
This is the advanced traverse function, which can be used used to write other variants of traversal and find.
Type signature for generator.next first argument is { skipChild?: boolean; skipSibling?: boolean } | void
Throw any error into the generator to finish the generator and let it cleanup its internals.
It allows inversion of control - so, application code can decide to
- change order of traversal,
- skip some elements,
- cancel traversal mid-way.
example
// Basic use (for-of)
const nodeIterator = traverseGenerator(rootNode);
for (const node of nodeIterator) {
// do something with each node here
}
example
// Breadth-first
// note the order below
const nodeIterator = traverseGenerator(rootNode, ["self", "sibling", "child"]);
// rest - same as above
example
// Get first 3 nodes and then stop the generator
const nodeIterator = traverseGenerator(rootNode);
var count = 0;
var next;
while (
count < 3 &&
!(next = nodeIterator.next()).done
) {
count++;
const node = next.value;
// do something with each node here
}
// Finish generator, to prevent memory leak
nodeIterator.throw(new Error());
Parameters:
▪ node: FiberNode
▪Default value __namedParameters: object= {}
| Name | Type | Default |
|---|---|---|
order |
"self" | "child" | "sibling"[] | ["self", "child", "sibling"] |
skipSelfForStartNode |
boolean | false |
skipSiblingForStartNode |
boolean | true |
Returns: IterableIterator<FiberNode>