RN: Prefer Destructured Import for useRef (#51404)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/51404

Prefers using this as a destructured import instead of as a member expression of `React`.

Changelog:
[Internal]

Reviewed By: SamChou19815

Differential Revision: D74893440

fbshipit-source-id: 9032f1e867a34b9cfa808f920a38f2630046eed7
This commit is contained in:
Tim Yung authored and Facebook GitHub Bot committed 2025-05-16 16:33:18 -07:00
1 parent 00108a1ac3
commit 1bf167ce97
22 files changed
+55 -51

No files matched your search

@@ -24,6 +24,7 @@ import LogBoxNotificationContainer from '../LogBox/LogBoxNotificationContainer';
import StyleSheet from '../StyleSheet/StyleSheet';
import {RootTagContext, createRootTag} from './RootTag';
import * as React from 'react';
import {useRef} from 'react';
const {useEffect, useState, useCallback} = React;
@@ -95,9 +96,9 @@ const AppContainer = ({
WrapperComponent,
rootViewStyle,
}: Props): React.Node => {
const appContainerRootViewRef: AppContainerRootViewRef = React.useRef(null);
const innerViewRef: InspectedViewRef = React.useRef(null);
const debuggingOverlayRef: DebuggingOverlayRef = React.useRef(null);
const appContainerRootViewRef: AppContainerRootViewRef = useRef(null);
const innerViewRef: InspectedViewRef = useRef(null);
const debuggingOverlayRef: DebuggingOverlayRef = useRef(null);
useSubscribeToDebuggingOverlayRegistry(
appContainerRootViewRef,
@@ -17,7 +17,7 @@ import RNTesterText from '../../components/RNTesterText';
import checkImageSource from './check.png';
import mixedCheckboxImageSource from './mixed.png';
import uncheckImageSource from './uncheck.png';
import React, {createRef, useState} from 'react';
import React, {createRef, useRef, useState} from 'react';
import {
AccessibilityInfo,
Alert,
@@ -1314,7 +1314,7 @@ class AnnounceForAccessibility extends React.Component<{}> {
}
function SetAccessibilityFocusExample(props: {}): React.Node {
const myRef = React.useRef<?React.ElementRef<typeof RNTesterText>>(null);
const myRef = useRef<?React.ElementRef<typeof RNTesterText>>(null);
const onPress = () => {
if (myRef && myRef.current) {
@@ -17,7 +17,7 @@ import RNTesterButton from '../../components/RNTesterButton';
import {RNTesterThemeContext} from '../../components/RNTesterTheme';
import ToggleNativeDriver from './utils/ToggleNativeDriver';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {
Animated,
FlatList,
@@ -148,10 +148,8 @@ function ComposingExampleItem({
// account the window width, box size, and padding
const maxXTranslation = windowWidth - boxSize - 4 * padding;
const boxIndexes = React.useMemo(() => [0, 1, 2, 3, 4], []);
const xTranslations = React.useRef(
boxIndexes.map(() => new Animated.Value(0)),
);
const animation = React.useRef(
const xTranslations = useRef(boxIndexes.map(() => new Animated.Value(0)));
const animation = useRef(
compositeAnimation(xTranslations.current, useNativeDriver),
);
const theme = React.useContext(RNTesterThemeContext);
@@ -15,7 +15,7 @@ import RNTesterButton from '../../components/RNTesterButton';
import {RNTesterThemeContext} from '../../components/RNTesterTheme';
import ToggleNativeDriver from './utils/ToggleNativeDriver';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {
Animated,
Easing,
@@ -87,8 +87,8 @@ function EasingItem({
item: EasingListItem,
useNativeDriver: boolean,
}): React.Node {
const opacityAndScale = React.useRef(new Animated.Value(1));
const animation = React.useRef(
const opacityAndScale = useRef(new Animated.Value(1));
const animation = useRef(
Animated.timing(opacityAndScale.current, {
toValue: 1,
duration: 1200,
@@ -14,7 +14,7 @@ import RNTConfigurationBlock from '../../components/RNTConfigurationBlock';
import RNTesterButton from '../../components/RNTesterButton';
import ToggleNativeDriver from './utils/ToggleNativeDriver';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {Animated, StyleSheet, Text, View} from 'react-native';
const containerWidth = 200;
@@ -59,7 +59,7 @@ const styles = StyleSheet.create({
type Props = $ReadOnly<{}>;
function MovingBoxView({useNativeDriver}: {useNativeDriver: boolean}) {
const x = React.useRef(new Animated.Value(0));
const x = useRef(new Animated.Value(0));
const [update, setUpdate] = useState(0);
const [boxVisible, setBoxVisible] = useState(true);
@@ -11,13 +11,13 @@
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {Animated, Button, Text, View} from 'react-native';
const componentList: number[] = Array.from({length: 100}, (_, i) => i + 1);
function PressableWithNativeDriver() {
const currScroll = React.useRef(new Animated.Value(0)).current;
const currScroll = useRef(new Animated.Value(0)).current;
const [count, setCount] = useState(0);
return (
@@ -12,7 +12,7 @@ import type {RNTesterModuleExample} from '../../../types/RNTesterTypes';
import ToggleNativeDriver from '../../Animated/utils/ToggleNativeDriver';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {Animated, StyleSheet, Text} from 'react-native';
const WIDTH = 200;
@@ -39,8 +39,8 @@ const styles = StyleSheet.create({
});
function CompatibilityAnimatedPointerMove(): React.Node {
const xCoord = React.useRef(new Animated.Value(0)).current;
const yCoord = React.useRef(new Animated.Value(0)).current;
const xCoord = useRef(new Animated.Value(0)).current;
const yCoord = useRef(new Animated.Value(0)).current;
const [useNativeDriver, setUseNativeDriver] = useState(true);
return (
@@ -11,7 +11,7 @@
import type {PointerEvent, ViewProps} from 'react-native';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {StyleSheet, Text, View} from 'react-native';
export default function EventfulView(props: {
@@ -36,7 +36,7 @@ export default function EventfulView(props: {
log: string => void,
...ViewProps,
}): React.Node {
const ref = React.useRef<?React.ElementRef<typeof View>>();
const ref = useRef<?React.ElementRef<typeof View>>();
React.useEffect(() => {
// $FlowFixMe[prop-missing] Using private property
setTag(ref.current?._nativeTag);
@@ -14,7 +14,7 @@ import type {ViewToken} from 'react-native/Libraries/Lists/ViewabilityHelper';
import BaseFlatListExample from './BaseFlatListExample';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {FlatList, StyleSheet, View} from 'react-native';
type FlatListProps = React.ElementProps<typeof FlatList>;
@@ -53,7 +53,7 @@ export function FlatList_BaseOnViewableItemsChanged(props: {
horizontal,
};
const ref = React.useRef<any>(null);
const ref = useRef<any>(null);
const onTest =
useScrollRefScroll === true
? () => {
@@ -14,7 +14,7 @@ import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
import BaseFlatListExample from './BaseFlatListExample';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
export function FlatList_onEndReached(): React.Node {
const [output, setOutput] = useState('');
@@ -23,7 +23,7 @@ export function FlatList_onEndReached(): React.Node {
setOutput('onEndReached'),
onEndReachedThreshold: 0,
};
const ref = React.useRef<any>(null);
const ref = useRef<any>(null);
const onTest = () => {
const scrollResponder = ref?.current?.getScrollResponder();
@@ -14,7 +14,7 @@ import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
import BaseFlatListExample, {ITEM_HEIGHT} from './BaseFlatListExample';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {FlatList} from 'react-native';
export function FlatList_onStartReached(): React.Node {
@@ -30,7 +30,7 @@ export function FlatList_onStartReached(): React.Node {
index,
}),
};
const ref = React.useRef<?FlatList<string>>(null);
const ref = useRef<?FlatList<string>>(null);
const onTest = () => {
ref.current?.scrollToOffset({offset: 0});
@@ -9,10 +9,12 @@
*/
'use strict';
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
import BaseFlatListExample from './BaseFlatListExample';
import * as React from 'react';
import {useRef} from 'react';
import {StyleSheet, Text, View} from 'react-native';
const Separator =
@@ -44,7 +46,7 @@ export function FlatList_withSeparators(): React.Node {
const exampleProps = {
ItemSeparatorComponent: Separator('lightgreen', 'green'),
};
const ref = React.useRef<$FlowFixMe>(null);
const ref = useRef<$FlowFixMe>(null);
return <BaseFlatListExample ref={ref} exampleProps={exampleProps} />;
}
@@ -15,13 +15,13 @@ import type {Node} from 'react';
import PopupMenuAndroid from '@react-native/popup-menu-android';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {Button, StyleSheet, Text, View} from 'react-native';
type Fruit = 'Apple' | 'Pear' | 'Banana' | 'Orange' | 'Kiwi';
const PopupMenu = () => {
const popupRef = React.useRef<?PopupMenuAndroidInstance>();
const popupRef = useRef<?PopupMenuAndroidInstance>();
const [fruit, setFruit] = useState<?Fruit>();
const fruits: Array<Fruit> = ['Apple', 'Pear', 'Banana', 'Orange', 'Kiwi'];
const items = fruits.map(item => ({
@@ -15,7 +15,7 @@ import RNTesterText from '../../components/RNTesterText';
import ScrollViewPressableStickyHeaderExample from './ScrollViewPressableStickyHeaderExample';
import nullthrows from 'nullthrows';
import * as React from 'react';
import {useCallback, useState} from 'react';
import {useCallback, useRef, useState} from 'react';
import {
Platform,
RefreshControl,
@@ -569,7 +569,7 @@ const HorizontalScrollView = (props: {
itemCount?: number,
}) => {
const {direction} = props;
const scrollRef = React.useRef<?React.ElementRef<typeof ScrollView>>();
const scrollRef = useRef<?React.ElementRef<typeof ScrollView>>();
const title = direction === 'ltr' ? 'LTR Layout' : 'RTL Layout';
const items =
props.itemCount == null ? ITEMS : ITEMS.slice(0, props.itemCount);
@@ -1013,7 +1013,7 @@ const KeyboardExample = () => {
const InvertStickyHeaders = () => {
const [invertStickyHeaders, setInvertStickyHeaders] = useState(false);
const _scrollView = React.useRef<?React.ElementRef<typeof ScrollView>>(null);
const _scrollView = useRef<?React.ElementRef<typeof ScrollView>>(null);
return (
<View>
{/* $FlowFixMe[incompatible-use] */}
@@ -1054,7 +1054,7 @@ const InvertStickyHeaders = () => {
};
const MultipleStickyHeaders = () => {
const _scrollView = React.useRef<?React.ElementRef<typeof ScrollView>>(null);
const _scrollView = useRef<?React.ElementRef<typeof ScrollView>>(null);
const stickyHeaderStyle = {backgroundColor: 'yellow'};
return (
<View>
@@ -9,7 +9,7 @@
*/
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {
Button,
Pressable,
@@ -52,7 +52,7 @@ function renderComponent1(i: number) {
}
export default function ScrollViewPressableStickyHeaderExample(): React.Node {
const scrollRef = React.useRef<$FlowFixMe>(null);
const scrollRef = useRef<$FlowFixMe>(null);
const components = [];
for (var i = 1; i < 10; i++) {
components.push(renderComponent1(i));
@@ -12,7 +12,7 @@ import type {ViewToken} from 'react-native/Libraries/Lists/ViewabilityHelper';
import SectionListBaseExample from './SectionListBaseExample';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {SectionList, StyleSheet, View} from 'react-native';
type SectionListProps = React.ElementProps<typeof SectionList>;
@@ -50,7 +50,7 @@ export function SectionList_BaseOnViewableItemsChanged(props: {
viewabilityConfig,
horizontal,
};
const ref = React.useRef<any>(null);
const ref = useRef<any>(null);
const onTest =
useScrollRefScroll === true
? () => {
@@ -10,7 +10,7 @@
import SectionListBaseExample from './SectionListBaseExample';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
export function SectionList_onEndReached(): React.Node {
const [output, setOutput] = useState('');
@@ -19,7 +19,7 @@ export function SectionList_onEndReached(): React.Node {
setOutput('onEndReached'),
onEndReachedThreshold: 0,
};
const ref = React.useRef<any>(null);
const ref = useRef<any>(null);
const onTest = () => {
const scrollResponder = ref?.current?.getScrollResponder();
@@ -28,7 +28,7 @@ import {
import RNTesterPage from '../../components/RNTesterPage';
import RNTesterText from '../../components/RNTesterText';
import React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {
Alert,
Animated,
@@ -213,7 +213,7 @@ export function SectionList_scrollable(Props: {...}): React.MixedElement {
setData([...data.slice(0, index), item, ...data.slice(index + 1)]);
};
const ref = React.useRef<?SectionList<SectionBase<any>>>(null);
const ref = useRef<?SectionList<SectionBase<any>>>(null);
const scrollToLocation = (sectionIndex: number, itemIndex: number) => {
// $FlowFixMe[method-unbinding] added when improving typing for this parameters
if (ref != null && ref.current?.scrollToLocation != null) {
@@ -10,6 +10,7 @@
import SectionListBaseExample from './SectionListBaseExample';
import * as React from 'react';
import {useRef} from 'react';
import {StyleSheet, Text, View} from 'react-native';
const Separator =
@@ -37,7 +38,7 @@ export function SectionList_withSeparators(): React.Node {
ItemSeparatorComponent: Separator('lightgreen', 'green', false),
SectionSeparatorComponent: Separator('lightblue', 'blue', true),
};
const ref = React.useRef<any>(null);
const ref = useRef<any>(null);
return <SectionListBaseExample ref={ref} exampleProps={exampleProps} />;
}
@@ -11,7 +11,7 @@
import type {ListRenderItemInfo} from 'react-native';
import * as React from 'react';
import {useState} from 'react';
import {useRef, useState} from 'react';
import {
Animated,
FlatList,
@@ -149,7 +149,7 @@ function Card(props: {color: string}) {
const separatorComponent = () => <View style={styles.separator} />;
const listRef = React.useRef<?FlatList<mixed>>();
const listRef = useRef<?FlatList<mixed>>();
React.useEffect(() => {
listRef.current?.scrollToOffset({offset: 0, animated: false});
@@ -20,6 +20,7 @@ import RNTesterText from '../../components/RNTesterText';
import ExampleTextInput from './ExampleTextInput';
import TextInputSharedExamples from './TextInputSharedExamples';
import React from 'react';
import {useRef} from 'react';
import {
Alert,
Button,
@@ -210,9 +211,9 @@ class SecureEntryExample extends React.Component<$FlowFixMeProps, any> {
}
const TextInputWithFocusButton = () => {
const inputToFocusRef = React.useRef<React.ElementRef<
typeof TextInput,
> | null>(null);
const inputToFocusRef = useRef<React.ElementRef<typeof TextInput> | null>(
null,
);
return (
<View>
<ExampleTextInput
@@ -8,7 +8,8 @@
* @flow
*/
import React, {useEffect, useState} from 'react';
import * as React from 'react';
import {useEffect, useRef, useState} from 'react';
import {Animated, Easing, StyleSheet, Text, View} from 'react-native';
function AnimateTransformSingleProp() {
@@ -49,7 +50,7 @@ function AnimateTransformSingleProp() {
}
function TransformOriginExample() {
const rotateAnim = React.useRef(new Animated.Value(0)).current;
const rotateAnim = useRef(new Animated.Value(0)).current;
useEffect(() => {
Animated.loop(