Skip to content

Commit a035c1f

Browse files
committed
Fix examples
1 parent c548a05 commit a035c1f

3 files changed

Lines changed: 23 additions & 26 deletions

File tree

docs/document-nodes.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,8 @@ export default function AccessingDocument() {
2222
const ref = React.useRef(null);
2323
2424
React.useEffect(() => {
25-
const element = ref.current;
26-
if (!element) {
27-
return;
28-
}
29-
3025
// Get the main text input in the screen and focus it after initial load.
26+
const element = ref.current;
3127
const doc = element.ownerDocument;
3228
const textInput = doc.getElementById('main-text-input');
3329
textInput?.focus();

docs/element-nodes.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@ import * as React from 'react';
1212
import { View, SafeAreaView, StyleSheet, Text } from 'react-native';
1313
1414
const ViewWithRefs = () => {
15+
const ref = React.useRef(null);
1516
const [viewInfo, setViewInfo] = React.useState('');
1617
18+
React.useEffect(() => {
19+
// `element` is an object implementing the interface described here.
20+
const element = ref.current;
21+
const rect = JSON.stringify(element.getBoundingClientRect());
22+
setViewInfo(
23+
`Bounding rect is: ${rect}.\nText content is: ${element.textContent}`,
24+
);
25+
}, []);
26+
1727
return (
1828
<SafeAreaView style={styles.container}>
19-
<View
20-
style={styles.content}
21-
ref={(instance) => {
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 () => {};
28-
}}
29-
>
29+
<View ref={ref} style={styles.content}>
3030
<Text>Hello world!</Text>
3131
</View>
3232
<Text>{viewInfo}</Text>

docs/text-nodes.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ import * as React from 'react';
1010
import { SafeAreaView, StyleSheet, Text } from 'react-native';
1111
1212
const TextWithRefs = () => {
13+
const ref = React.useRef(null);
1314
const [viewInfo, setViewInfo] = React.useState('');
1415
16+
React.useEffect(() => {
17+
// `textElement` is an object implementing the interface described here.
18+
const textElement = ref.current;
19+
const textNode = textElement.childNodes[0];
20+
setViewInfo(
21+
`Text content is: ${textNode.nodeValue}`,
22+
);
23+
}, []);
24+
1525
return (
1626
<SafeAreaView style={styles.container}>
17-
<Text
18-
ref={(instance) => {
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 () => {};
25-
}}
26-
>
27+
<Text ref={ref}>
2728
Hello world!
2829
</Text>
2930
<Text>{viewInfo}</Text>

0 commit comments

Comments
 (0)