Skip to content

TypeScript: Replace deprecated React.ElementRef usages to React.ComponentRef#50883

Closed
mateoguzmana wants to merge 1 commit into
facebook:mainfrom
mateoguzmana:feat/replace-deprecated-element-ref
Closed

TypeScript: Replace deprecated React.ElementRef usages to React.ComponentRef#50883
mateoguzmana wants to merge 1 commit into
facebook:mainfrom
mateoguzmana:feat/replace-deprecated-element-ref

Conversation

@mateoguzmana
Copy link
Copy Markdown
Collaborator

@mateoguzmana mateoguzmana commented Apr 23, 2025

Summary:

While upgrading a project to React 19, I noticed React.ElementRef is deprecated (see types/react/index.d.ts#L199). I think we can replace it for the RN types as well.

Not sure if this is considered as a breaking change.

Changelog:

[GENERAL] [CHANGED] - TypeScript: Replace deprecated React.ElementRef usages to React.ComponentRef

Test Plan:

Create a RNTesterPlayground.tsx next to the normal .js just to validate the type checking is not throwing an unexpected error.

Code snippet:
import React, { useRef, useEffect } from 'react';
import { FlatList, Text, View } from 'react-native';

type Item = { id: string; title: string };

const data: Item[] = Array.from({ length: 10 }, (_, i) => ({
  id: i.toString(),
  title: `Item ${i + 1}`,
}));

const FlatListScrollRefExample: React.FC = () => {
  const flatListRef = useRef<FlatList<Item>>(null);

  useEffect(() => {
    if (flatListRef.current) {
      const nativeRef = flatListRef.current.getNativeScrollRef();

      console.log('nativeRef', nativeRef?.componentWillUnmount);
    }
  }, []);

  return (
    <FlatList
      ref={flatListRef}
      data={data}
      keyExtractor={(item) => item.id}
      renderItem={({ item }) => (
        <View style={{ padding: 16 }}>
          <Text>{item.title}</Text>
        </View>
      )}
    />
  );
};

export default FlatListScrollRefExample;

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 23, 2025
@mateoguzmana mateoguzmana marked this pull request as ready for review April 23, 2025 23:00
@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Apr 23, 2025
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@rshest has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Apr 24, 2025
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@rshest merged this pull request in 12147e3.

@react-native-bot
Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @mateoguzmana in 12147e3

When will my fix make it into a release? | How to file a pick request?

@mikehardy
Copy link
Copy Markdown
Contributor

This didn't go far enough - the deprecated React.ElementRef is still a strict requirement in the codegen phase:

https://github.com/search?q=repo%3Afacebook%2Freact-native%20React.ElementRef%3C%3E&type=code

I suppose I should log a new issue as there isn't one currently

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants