Skip to content

Commit c548a05

Browse files
committed
Address feedback about examples and clarify Text component refs
1 parent d4cee9f commit c548a05

4 files changed

Lines changed: 36 additions & 33 deletions

File tree

docs/document-nodes.md

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,34 @@ Document nodes represent a complete native view tree. Apps using native navigati
77

88
```SnackPlayer ext=js&name=Document%20instance%20example
99
import * as React from 'react';
10-
import {Text, View} from 'react-native';
10+
import {Text, TextInput, View} from 'react-native';
1111
12-
function countNodes(node) {
13-
let count = 1;
14-
for (const child of node.childNodes) {
15-
count += countNodes(child);
16-
}
17-
return count;
12+
function MyComponent(props) {
13+
return (
14+
<View ref={props.ref}>
15+
<Text>Start typing below</Text>
16+
<TextInput id="main-text-input" />
17+
</View>
18+
)
1819
}
1920
2021
export default function AccessingDocument() {
21-
const [nodeCount, setNodeCount] = React.useState(0);
22+
const ref = React.useRef(null);
23+
24+
React.useEffect(() => {
25+
const element = ref.current;
26+
if (!element) {
27+
return;
28+
}
29+
30+
// Get the main text input in the screen and focus it after initial load.
31+
const doc = element.ownerDocument;
32+
const textInput = doc.getElementById('main-text-input');
33+
textInput?.focus();
34+
}, []);
2235
2336
return (
24-
<View ref={instance => {
25-
if (instance != null) {
26-
// or instance.getRootNode()
27-
const document = instance.ownerDocument;
28-
setNodeCount(countNodes(document));
29-
}
30-
}}>
31-
<Text>Number of nodes in tree: {nodeCount}</Text>
32-
</View>
37+
<MyComponent ref={ref} />
3338
);
3439
}
3540
```

docs/element-nodes.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ const ViewWithRefs = () => {
1919
<View
2020
style={styles.content}
2121
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-
}
22+
// `instance` is an object implementing the interface described here.
23+
const rect = JSON.stringify(instance.getBoundingClientRect());
24+
setViewInfo(
25+
`Bounding rect is: ${rect}.\nText content is: ${instance.textContent}`,
26+
);
27+
return () => {};
3028
}}
3129
>
3230
<Text>Hello world!</Text>

docs/text-nodes.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ const TextWithRefs = () => {
1616
<SafeAreaView style={styles.container}>
1717
<Text
1818
ref={(instance) => {
19-
// `instance` is an object implementing the interface described here,
20-
// or `null` when the component is unmounted.
21-
if (instance != null) {
22-
const textNode = instance.childNodes[0];
23-
setViewInfo(
24-
`Text content is: ${textNode.nodeValue}`,
25-
);
26-
}
19+
// `instance` is an object implementing the interface described here.
20+
const textNode = instance.childNodes[0];
21+
setViewInfo(
22+
`Text content is: ${textNode.nodeValue}`,
23+
);
24+
return () => {};
2725
}}
2826
>
2927
Hello world!

docs/text.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,8 @@ When the scroll view is disabled, this defines how far your touch may move off o
642642

643643
A ref setter that will be assigned an [element node](element-nodes) when mounted.
644644

645+
Note that `Text` components don't provide text nodes, the same way that paragraph elements (`<p>`) on Web are element nodes instead of text nodes. Text nodes can be found as their child nodes instead.
646+
645647
---
646648

647649
### `role`

0 commit comments

Comments
 (0)