来自 refs 的节点
React Native 应用会渲染一个原生视图树来表示 UI,类似于 React DOM 在 Web 上的呈现方式(DOM 树)。React Native 通过 refs 提供对此树的命令式访问,所有原生组件(包括由 View
等内置组件渲染的组件)都会返回这些节点。
¥React Native apps render a native view tree that represents the UI, similar to how React DOM does on Web (the DOM tree). React Native provides imperative access to this tree via refs, which are returned by all native components (including those rendered by built-in components like View
).
React Native 提供三种类型的节点:
¥React Native provides 3 types of nodes:
-
元素:元素节点代表原生视图树中的原生组件(类似于 Web 上的 元素 节点)。所有原生组件都通过引用提供它们。
¥Elements: element nodes represent native components in the native view tree (similar to Element nodes on Web). They are provided by all native components via refs.
-
Text:文本节点代表树上的原始文本内容(类似于 Web 上的
Text
节点)。它们无法通过refs
直接访问,但可以通过元素引用上的childNodes
等方法访问。¥Text: text nodes represent raw text content on the tree (similar to
Text
nodes on Web). They are not directly accessible viarefs
, but can be accessed using methods likechildNodes
on element refs. -
文档:文档节点代表完整的原生视图树(类似于 Web 上的
Document
节点)。与文本节点一样,它们只能通过其他节点使用类似ownerDocument
的属性进行访问。¥Documents: document nodes represent a complete native view tree (similar to
Document
nodes on Web). Like text nodes, they can only be accessed through other nodes, using properties likeownerDocument
.
与 Web 一样,这些节点可用于遍历渲染的 UI 树、访问布局信息或执行类似 focus
的命令式操作。
¥As on Web, these nodes can be used to traverse the rendered UI tree, access layout information or execute imperative operations like focus
.
与 Web 不同,这些节点不允许修改(例如:node.appendChild
),因为树内容完全由 React 渲染器管理。
¥Unlike on Web, these nodes do not allow mutation (e.g.: node.appendChild
), as the tree contents are fully managed by the React renderer.