Files
yoga/website/docs/advanced/incremental-layout.mdx
J8118andmeta-codesync[bot] 070581edf3 Fix incorrect code examples in documentation (#1926)
Summary:
Two small fixes to code examples in the documentation:

- **`external-layout-systems.mdx`:** The C/C++ tab declares `Widget widget{};` on line 24 but then references `&w` on line 26. Fixed to `&widget` to match the declared variable. The Java tab already uses the correct name (`widget`).

- **`incremental-layout.mdx`:** The JavaScript tab uses `void applyLayout(node) {` which is not valid JavaScript syntax. Fixed to `function applyLayout(node) {`. This appears to have been introduced in https://github.com/facebook/yoga/issues/1631 when the JS example was added by adapting the Java tab — other language-specific syntax was updated (`int` → `let`, type annotations removed) but the `void` return type was not changed to `function`. The code block is tagged as ` ```javascript ` and all other lines use valid JS syntax, confirming this was a copy-paste oversight rather than intentional pseudocode.

Pull Request resolved: https://github.com/facebook/yoga/pull/1926

Reviewed By: NickGerleman

Differential Revision: D100143877

Pulled By: fabriziocucci

fbshipit-source-id: 6d7a88d044ca369fa69046beb58c2e5f1256a27c
2026-04-10 03:56:20 -07:00

88 lines
2.2 KiB
Plaintext

---
sidebar_position: 1
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
# Incremental layout
Most real-world UIs are not static. Yoga trees may be laid out incrementally to avoid large work when only a small portion of the UI changes. Yoga will automatically mark a node and its ancestors as "dirty" if the node's style or children are changed. During the first layout, all nodes are considered dirty, and every node is visited. On subsequent layout, any nodes that are not dirty are skipped, so long as their parent constraints have not changed.
Yoga exposes whether the layout of a node or its children have changed via a `HasNewLayout` flag. This flag may be read when applying Yoga layout results to avoid traversing any subtrees where there are no updates.
<Tabs groupId="language">
<TabItem value="cpp" label="C/C++">
```cpp
void applyLayout(YGNodeRef node) {
if (!YGNodeHasNewLayout(node)) {
return;
}
// Reset the flag
YGNodeSetHasNewLayout(node, false);
// Do the real work
...
for (size_t i = 0; i < YGNodeGetChildCount(node); i++) {
applyLayout(YGNodeGetChild(node, i));
}
}
```
</TabItem>
<TabItem value="java" label="Java">
```java
void applyLayout(YogaNode node) {
if (!node.hasNewLayout()) {
return;
}
// Reset the flag
node.markLayoutSeen();
// Do the real work
...
for (int i = 0; i < node.getChildCount(); i++) {
applyLayout(node.getChildAt(i));
}
}
```
</TabItem>
<TabItem value="js" label="JavaScript">
```javascript
function applyLayout(node) {
if (!node.hasNewLayout()) {
return;
}
// Reset the flag
node.markLayoutSeen();
// Do the real work
...
for (let i = 0; i < node.getChildCount(); i++) {
applyLayout(node.getChildAt(i));
}
}
```
</TabItem>
</Tabs>
In the below example, we start with an already laid out tree. The style of `F` is then modified, dirtying the node and its ancestors. After layout, the dirtied nodes, and any effected children, are marked as having new layout.
| Clean tree | Dirtied tree | Has new layout |
| - | - | - |
| ![Clean tree](../img/invalidation-clean-tree.svg) | ![Clean tree](../img/invalidation-dirtied-tree.svg) | ![Has new layout](../img/invalidation-new-layout-tree.svg) |