|
| 1 | +--- |
| 2 | +id: element-nodes |
| 3 | +title: Element nodes |
| 4 | +--- |
| 5 | + |
| 6 | +Element nodes represent native components in the native view tree (similar to [Element](https://developer.mozilla.org/en-US/docs/Web/API/Element) nodes on Web). |
| 7 | + |
| 8 | +They are provided by all native components, and by many built-in components, via refs: |
| 9 | + |
| 10 | +```SnackPlayer ext=js&name=Element%20instances%20example |
| 11 | +import * as React from 'react'; |
| 12 | +import { View, SafeAreaView, StyleSheet, Text } from 'react-native'; |
| 13 | +
|
| 14 | +const ViewWithRefs = () => { |
| 15 | + const [viewInfo, setViewInfo] = React.useState(''); |
| 16 | +
|
| 17 | + return ( |
| 18 | + <SafeAreaView style={styles.container}> |
| 19 | + <View |
| 20 | + style={styles.content} |
| 21 | + ref={(instance) => { |
| 22 | + // `instance` is an object implementing the interface described here, |
| 23 | + // or `null` when the component is unmounted. |
| 24 | + if (instance != null) { |
| 25 | + const rect = JSON.stringify(instance.getBoundingClientRect()); |
| 26 | + setViewInfo( |
| 27 | + `Bounding rect is: ${rect}.\nText content is: ${instance.textContent}`, |
| 28 | + ); |
| 29 | + } |
| 30 | + }} |
| 31 | + > |
| 32 | + <Text>Hello world!</Text> |
| 33 | + </View> |
| 34 | + <Text>{viewInfo}</Text> |
| 35 | + </SafeAreaView> |
| 36 | + ); |
| 37 | +}; |
| 38 | +
|
| 39 | +const styles = StyleSheet.create({ |
| 40 | + container: { |
| 41 | + flex: 1, |
| 42 | + }, |
| 43 | + content: { |
| 44 | + padding: 10, |
| 45 | + backgroundColor: 'gray', |
| 46 | + }, |
| 47 | +}); |
| 48 | +
|
| 49 | +export default ViewWithRefs; |
| 50 | +``` |
| 51 | + |
| 52 | +Note that some built-in components are just a container for other components (including native components). For example, `ScrollView` internally renders a native scroll view and a native view, which are accessible through the ref is provides using methods like `getNativeScrollRef()` and `getInnerViewRef()`. |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## Reference |
| 57 | + |
| 58 | +### Web-compatible API |
| 59 | + |
| 60 | +From [`HTMLElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement): |
| 61 | + |
| 62 | +- Properties |
| 63 | + - [`offsetHeight`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetHeight) |
| 64 | + - [`offsetLeft`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetLeft) |
| 65 | + - [`offsetParent`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetParent) |
| 66 | + - [`offsetTop`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetTop) |
| 67 | + - [`offsetWidth`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth) |
| 68 | +- Methods |
| 69 | + - [`blur()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/blur). |
| 70 | + - ℹ️ This method was also [available](/docs/next/legacy/direct-manipulation#blur) in the legacy architecture. |
| 71 | + - [`focus()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus). |
| 72 | + - ℹ️ This method was also [available](/docs/next/legacy/direct-manipulation#focus) in the legacy architecture. |
| 73 | + - ⚠️ The `options` parameter is not supported. |
| 74 | + |
| 75 | +From [`Element`](https://developer.mozilla.org/en-US/docs/Web/API/Element): |
| 76 | + |
| 77 | +- Properties |
| 78 | + - [`childElementCount`](https://developer.mozilla.org/en-US/docs/Web/API/Element/childElementCount) |
| 79 | + - [`children`](https://developer.mozilla.org/en-US/docs/Web/API/Element/children) |
| 80 | + - [`clientHeight`](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight) |
| 81 | + - [`clientLeft`](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientLeft) |
| 82 | + - [`clientTop`](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientTop) |
| 83 | + - [`clientWidth`](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth) |
| 84 | + - [`firstElementChild`](https://developer.mozilla.org/en-US/docs/Web/API/Element/firstElementChild) |
| 85 | + - [`id`](https://developer.mozilla.org/en-US/docs/Web/API/Element/id) |
| 86 | + - ℹ️ Returns the value of the `id` or `nativeID` props. |
| 87 | + - [`lastElementChild`](https://developer.mozilla.org/en-US/docs/Web/API/Element/lastElementChild) |
| 88 | + - [`nextElementSibling`](https://developer.mozilla.org/en-US/docs/Web/API/Element/nextElementSibling) |
| 89 | + - [`nodeName`](https://developer.mozilla.org/en-US/docs/Web/API/Element/nodeName) |
| 90 | + - [`nodeType`](https://developer.mozilla.org/en-US/docs/Web/API/Element/nodeType) |
| 91 | + - [`nodeValue`](https://developer.mozilla.org/en-US/docs/Web/API/Element/nodeValue) |
| 92 | + - [`previousElementSibling`](https://developer.mozilla.org/en-US/docs/Web/API/Element/previousElementSibling) |
| 93 | + - [`scrollHeight`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight) |
| 94 | + - [`scrollLeft`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft) |
| 95 | + - ⚠️ For built-in components, only `ScrollView` instances can return a value other than zero. |
| 96 | + - [`scrollTop`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop) |
| 97 | + - ⚠️ For built-in components, only `ScrollView` instances can return a value other than zero. |
| 98 | + - [`scrollWidth`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollWidth) |
| 99 | + - [`tagName`](https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName) |
| 100 | + - ℹ️ Returns a normalized native component name prefixed with `RN:`, like `RN:View`. |
| 101 | + - [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Element/textContent) |
| 102 | +- Methods |
| 103 | + - [`getBoundingClientRect()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) |
| 104 | + - [`hasPointerCapture()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/hasPointerCapture) |
| 105 | + - [`setPointerCapture()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/setPointerCapture) |
| 106 | + - [`releasePointerCapture()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/releasePointerCapture) |
| 107 | + |
| 108 | +From [`Node`](https://developer.mozilla.org/en-US/docs/Web/API/Node): |
| 109 | + |
| 110 | +- Properties |
| 111 | + - [`childNodes`](https://developer.mozilla.org/en-US/docs/Web/API/Node/childNodes) |
| 112 | + - [`firstChild`](https://developer.mozilla.org/en-US/docs/Web/API/Node/firstChild) |
| 113 | + - [`isConnected`](https://developer.mozilla.org/en-US/docs/Web/API/Node/isConnected) |
| 114 | + - [`lastChild`](https://developer.mozilla.org/en-US/docs/Web/API/Node/lastChild) |
| 115 | + - [`nextSibling`](https://developer.mozilla.org/en-US/docs/Web/API/Node/nextSibling) |
| 116 | + - [`nodeName`](https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName) |
| 117 | + - [`nodeType`](https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType) |
| 118 | + - [`nodeValue`](https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue) |
| 119 | + - [`ownerDocument`](https://developer.mozilla.org/en-US/docs/Web/API/Node/ownerDocument) |
| 120 | + - ℹ️ Will return the [document instance](/docs/next/document-instances) where this component was rendered. |
| 121 | + - [`parentElement`](https://developer.mozilla.org/en-US/docs/Web/API/Node/parentElement) |
| 122 | + - [`parentNode`](https://developer.mozilla.org/en-US/docs/Web/API/Node/parentNode) |
| 123 | + - [`previousSibling`](https://developer.mozilla.org/en-US/docs/Web/API/Node/previousSibling) |
| 124 | + - [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) |
| 125 | +- Methods |
| 126 | + - [`compareDocumentPosition()`](https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition) |
| 127 | + - [`contains()`](https://developer.mozilla.org/en-US/docs/Web/API/Node/contains) |
| 128 | + - [`getRootNode()`](https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode) |
| 129 | + - ℹ️ Will return a reference to itself if the component is not mounted. |
| 130 | + - [`hasChildNodes()`](https://developer.mozilla.org/en-US/docs/Web/API/Node/hasChildNodes) |
| 131 | + |
| 132 | +### Legacy API |
| 133 | + |
| 134 | +- [`measure()`](/docs/next/legacy/direct-manipulation#measurecallback) |
| 135 | +- [`measureInWindow()`](/docs/next/legacy/direct-manipulation#measureinwindowcallback) |
| 136 | +- [`measureLayout()`](/docs/next/legacy/direct-manipulation#measurelayoutrelativetonativecomponentref-onsuccess-onfail) |
| 137 | +- [`setNativeProps()`](/docs/next/legacy/direct-manipulation#setnativeprops-with-touchableopacity) |
0 commit comments