|
| 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 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 style={styles.content} ref={instance => { |
| 20 | + // `instance` is an object implementing the interface described here, |
| 21 | + // or `null` when the component is unmounted. |
| 22 | + if (instance != null) { |
| 23 | + const rect = JSON.stringify(instance.getBoundingClientRect()); |
| 24 | + setViewInfo(`Bounding rect is: ${rect}.\nText content is: ${instance.textContent}`); |
| 25 | + } |
| 26 | + }}> |
| 27 | + <Text>Hello world!</Text> |
| 28 | + </View> |
| 29 | + <Text>{viewInfo}</Text> |
| 30 | + </SafeAreaView> |
| 31 | + ); |
| 32 | +}; |
| 33 | +
|
| 34 | +const styles = StyleSheet.create({ |
| 35 | + container: { |
| 36 | + flex: 1, |
| 37 | + }, |
| 38 | + content: { |
| 39 | + padding: 10, |
| 40 | + backgroundColor: 'gray', |
| 41 | + } |
| 42 | +}); |
| 43 | +
|
| 44 | +export default ViewWithRefs; |
| 45 | +``` |
| 46 | + |
| 47 | +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()`. |
| 48 | + |
| 49 | +--- |
| 50 | + |
| 51 | +## Reference |
| 52 | + |
| 53 | +### Web-compatible API |
| 54 | + |
| 55 | +From [`HTMLElement`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement): |
| 56 | + |
| 57 | +- Properties |
| 58 | + - [`offsetHeight`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetHeight) |
| 59 | + - [`offsetLeft`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetLeft) |
| 60 | + - [`offsetParent`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetParent) |
| 61 | + - [`offsetTop`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetTop) |
| 62 | + - [`offsetWidth`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/offsetWidth) |
| 63 | +- Methods |
| 64 | + - [`blur`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/blur). |
| 65 | + - This method was also [available](/docs/next/legacy/direct-manipulation#blur) in the legacy architecture. |
| 66 | + - [`focus`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus). |
| 67 | + - This method was also [available](/docs/next/legacy/direct-manipulation#focus) in the legacy architecture. |
| 68 | + - The `options` parameter is not supported. |
| 69 | + |
| 70 | +From [`Element`](https://developer.mozilla.org/en-US/docs/Web/API/Element): |
| 71 | + |
| 72 | +- Properties |
| 73 | + - [`childElementCount`](https://developer.mozilla.org/en-US/docs/Web/API/Element/childElementCount) |
| 74 | + - [`children`](https://developer.mozilla.org/en-US/docs/Web/API/Element/children) |
| 75 | + - [`clientHeight`](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientHeight) |
| 76 | + - [`clientLeft`](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientLeft) |
| 77 | + - [`clientTop`](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientTop) |
| 78 | + - [`clientWidth`](https://developer.mozilla.org/en-US/docs/Web/API/Element/clientWidth) |
| 79 | + - [`firstElementChild`](https://developer.mozilla.org/en-US/docs/Web/API/Element/firstElementChild) |
| 80 | + - [`id`](https://developer.mozilla.org/en-US/docs/Web/API/Element/id) |
| 81 | + - Returns the value of the `id` or `nativeID` props. |
| 82 | + - [`lastElementChild`](https://developer.mozilla.org/en-US/docs/Web/API/Element/lastElementChild) |
| 83 | + - [`nextElementSibling`](https://developer.mozilla.org/en-US/docs/Web/API/Element/nextElementSibling) |
| 84 | + - [`nodeName`](https://developer.mozilla.org/en-US/docs/Web/API/Element/nodeName) |
| 85 | + - [`nodeType`](https://developer.mozilla.org/en-US/docs/Web/API/Element/nodeType) |
| 86 | + - [`nodeValue`](https://developer.mozilla.org/en-US/docs/Web/API/Element/nodeValue) |
| 87 | + - [`previousElementSibling`](https://developer.mozilla.org/en-US/docs/Web/API/Element/previousElementSibling) |
| 88 | + - [`scrollHeight`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight) |
| 89 | + - [`scrollLeft`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollLeft) |
| 90 | + - For built-in components, only `ScrollView` instances can return a value other than zero. |
| 91 | + - [`scrollTop`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop) |
| 92 | + - For built-in components, only `ScrollView` instances can return a value other than zero. |
| 93 | + - [`scrollWidth`](https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollWidth) |
| 94 | + - [`tagName`](https://developer.mozilla.org/en-US/docs/Web/API/Element/tagName) |
| 95 | + - Returns a normalized native component name prefixed with `RN:`, like `RN:View`. |
| 96 | + - [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Element/textContent) |
| 97 | +- Methods |
| 98 | + - [`getBoundingClientRect`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) |
| 99 | + - [`hasPointerCapture`](https://developer.mozilla.org/en-US/docs/Web/API/Element/hasPointerCapture) |
| 100 | + - [`setPointerCapture`](https://developer.mozilla.org/en-US/docs/Web/API/Element/setPointerCapture) |
| 101 | + - [`releasePointerCapture`](https://developer.mozilla.org/en-US/docs/Web/API/Element/releasePointerCapture) |
| 102 | + |
| 103 | +From [`Node`](https://developer.mozilla.org/en-US/docs/Web/API/Node): |
| 104 | + |
| 105 | +- Properties |
| 106 | + - [`childNodes`](https://developer.mozilla.org/en-US/docs/Web/API/Node/childNodes) |
| 107 | + - [`firstChild`](https://developer.mozilla.org/en-US/docs/Web/API/Node/firstChild) |
| 108 | + - [`isConnected`](https://developer.mozilla.org/en-US/docs/Web/API/Node/isConnected) |
| 109 | + - [`lastChild`](https://developer.mozilla.org/en-US/docs/Web/API/Node/lastChild) |
| 110 | + - [`nextSibling`](https://developer.mozilla.org/en-US/docs/Web/API/Node/nextSibling) |
| 111 | + - [`nodeName`](https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeName) |
| 112 | + - [`nodeType`](https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeType) |
| 113 | + - [`nodeValue`](https://developer.mozilla.org/en-US/docs/Web/API/Node/nodeValue) |
| 114 | + - [`ownerDocument`](https://developer.mozilla.org/en-US/docs/Web/API/Node/ownerDocument) |
| 115 | + - Will return the [document instance](/docs/next/document-instances) where this component was rendered. |
| 116 | + - [`parentElement`](https://developer.mozilla.org/en-US/docs/Web/API/Node/parentElement) |
| 117 | + - [`parentNode`](https://developer.mozilla.org/en-US/docs/Web/API/Node/parentNode) |
| 118 | + - [`previousSibling`](https://developer.mozilla.org/en-US/docs/Web/API/Node/previousSibling) |
| 119 | + - [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent) |
| 120 | +- Methods |
| 121 | + - [`compareDocumentPosition`](https://developer.mozilla.org/en-US/docs/Web/API/Node/compareDocumentPosition) |
| 122 | + - [`contains`](https://developer.mozilla.org/en-US/docs/Web/API/Node/contains) |
| 123 | + - [`getRootNode`](https://developer.mozilla.org/en-US/docs/Web/API/Node/getRootNode) |
| 124 | + - [`hasChildNodes`](https://developer.mozilla.org/en-US/docs/Web/API/Node/hasChildNodes) |
| 125 | + |
| 126 | +### Legacy API |
| 127 | + |
| 128 | +- [`measure`](/docs/next/legacy/direct-manipulation#measurecallback) |
| 129 | +- [`measureInWindow`](/docs/next/legacy/direct-manipulation#measureinwindowcallback) |
| 130 | +- [`measureLayout`](/docs/next/legacy/direct-manipulation#measurelayoutrelativetonativecomponentref-onsuccess-onfail) |
| 131 | +- [`setNativeProps`](/docs/next/legacy/direct-manipulation#setnativeprops-with-touchableopacity) |
0 commit comments