Skip to content

Commit 797705c

Browse files
SamChou19815facebook-github-bot
authored andcommitted
Make use of ref-as-prop support in examples (#51358)
Summary: Pull Request resolved: #51358 Make use of the React 19 feature so that we can remove the remaining `forwardRef` in react native. Changelog: [Internal] Reviewed By: yungsters Differential Revision: D74812747 fbshipit-source-id: 7ded547ff62ca59d28abfc46a2f57466e2486acd
1 parent d07b2a9 commit 797705c

3 files changed

Lines changed: 51 additions & 41 deletions

File tree

packages/rn-tester/js/examples/FlatList/BaseFlatListExample.js

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -70,42 +70,39 @@ type Props = $ReadOnly<{
7070
const BaseFlatListExample: component(
7171
ref: React.RefSetter<FlatList<string>>,
7272
...props: Props
73-
) = React.forwardRef(
74-
// $FlowFixMe[incompatible-call]
75-
(props: Props, ref) => {
76-
return (
77-
<View style={styles.container}>
78-
{props.testOutput != null ? (
79-
<View testID="test_container" style={styles.testContainer}>
80-
<Text style={styles.output} numberOfLines={1} testID="output">
81-
{props.testOutput}
82-
</Text>
83-
{props.onTest != null ? (
84-
<Button
85-
testID="start_test"
86-
onPress={props.onTest}
87-
title={props.testLabel ?? 'Test'}
88-
/>
89-
) : null}
90-
</View>
91-
) : null}
92-
{props.children}
93-
<FlatList
94-
{...props.exampleProps}
95-
// $FlowFixMe[incompatible-type]
96-
ref={ref}
97-
testID="flat_list"
98-
// $FlowFixMe[incompatible-type]
99-
data={DATA}
100-
keyExtractor={(item, index) => item + index}
101-
style={styles.list}
102-
// $FlowFixMe[incompatible-type-arg]
103-
renderItem={Item}
104-
/>
105-
</View>
106-
);
107-
},
108-
);
73+
) = ({ref, ...props}: {ref: React.RefSetter<FlatList<string>>, ...Props}) => {
74+
return (
75+
<View style={styles.container}>
76+
{props.testOutput != null ? (
77+
<View testID="test_container" style={styles.testContainer}>
78+
<Text style={styles.output} numberOfLines={1} testID="output">
79+
{props.testOutput}
80+
</Text>
81+
{props.onTest != null ? (
82+
<Button
83+
testID="start_test"
84+
onPress={props.onTest}
85+
title={props.testLabel ?? 'Test'}
86+
/>
87+
) : null}
88+
</View>
89+
) : null}
90+
{props.children}
91+
<FlatList
92+
{...props.exampleProps}
93+
// $FlowFixMe[incompatible-type]
94+
ref={ref}
95+
testID="flat_list"
96+
// $FlowFixMe[incompatible-type]
97+
data={DATA}
98+
keyExtractor={(item, index) => item + index}
99+
style={styles.list}
100+
// $FlowFixMe[incompatible-type-arg]
101+
renderItem={Item}
102+
/>
103+
</View>
104+
);
105+
};
109106

110107
export default BaseFlatListExample;
111108

packages/rn-tester/js/examples/SectionList/SectionListBaseExample.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,14 @@ const SectionListBaseExample: component(
7777
// $FlowIgnore[unclear-type]
7878
ref: React.RefSetter<SectionList<any>>,
7979
...props: Props
80-
) = React.forwardRef((props: Props, ref): React.Node => {
80+
) = ({
81+
ref,
82+
...props
83+
}: {
84+
// $FlowIgnore[unclear-type]
85+
ref: React.RefSetter<SectionList<any>>,
86+
...Props,
87+
}): React.Node => {
8188
return (
8289
<View style={styles.container}>
8390
{props.testOutput != null ? (
@@ -112,7 +119,7 @@ const SectionListBaseExample: component(
112119
/>
113120
</View>
114121
);
115-
});
122+
};
116123

117124
const styles = StyleSheet.create({
118125
item: {

packages/rn-tester/js/examples/TextInput/ExampleTextInput.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@
1010
*/
1111

1212
import {RNTesterThemeContext} from '../../components/RNTesterTheme';
13-
import React, {forwardRef, useContext} from 'react';
13+
import React, {useContext} from 'react';
1414
import {StyleSheet, TextInput} from 'react-native';
1515

1616
const ExampleTextInput: component(
1717
ref: React.RefSetter<null | React.ElementRef<typeof TextInput>>,
1818
...props: React.ElementConfig<typeof TextInput>
19-
) = forwardRef((props, ref) => {
19+
) = ({
20+
ref,
21+
...props
22+
}: {
23+
ref?: React.RefSetter<null | React.ElementRef<typeof TextInput>>,
24+
...React.ElementConfig<typeof TextInput>,
25+
}) => {
2026
const theme = useContext(RNTesterThemeContext);
2127

2228
return (
@@ -34,7 +40,7 @@ const ExampleTextInput: component(
3440
]}
3541
/>
3642
);
37-
});
43+
};
3844

3945
const styles = StyleSheet.create({
4046
input: {

0 commit comments

Comments
 (0)