Skip to content

chore: [TS] Transform TouchableOpacity from class to ForwardRef component#44030

Closed
retyui wants to merge 1 commit intofacebook:mainfrom
retyui:chore/retyui/touchable-not-a-class
Closed

chore: [TS] Transform TouchableOpacity from class to ForwardRef component#44030
retyui wants to merge 1 commit intofacebook:mainfrom
retyui:chore/retyui/touchable-not-a-class

Conversation

@retyui
Copy link
Copy Markdown
Contributor

@retyui retyui commented Apr 10, 2024

Summary:

If you check the source of truth packages/react-native/Libraries/Components/Touchable/TouchableOpacity.js I'll find that TouchableOpacity is a result of React.forwardRef(...) :

const Touchable: React.AbstractComponent<
Props,
React.ElementRef<typeof Animated.View>,
> = React.forwardRef((props, ref) => (
<TouchableOpacity {...props} hostRef={ref} />
));
Touchable.displayName = 'TouchableOpacity';
module.exports = Touchable;

So the TS type isn't correct : (

<TouchableOpacity ref={ref => {   }} />
//                     ^^^ ref should be a `View` (but now it's `TouchableOpacity`)

Breaking changes

As TouchableOpacity isn't class anymore it can't be used as value & type

import {TouchableOpacity} from 'react-native';
const ref = useRef<TouchableOpacity>();
//                ^^^ TS2749: TouchableOpacity refers to a value, but is being used as a type here.
//                            Did you mean typeof TouchableOpacity?

Recommend solution: use build-in react type React.ElementRef

-const ref = useRef<TouchableOpacity>();
+const ref = useRef<React.ElementRef<typeof TouchableOpacity>>();

Also, it possible to use View as type:

-const ref = useRef<TouchableOpacity>();
+const ref = useRef<View>();

Changelog:

[GENERAL] [BREAKING] - [Typescript] Transform TouchableOpacity from JS class to ForwardRef component

Test Plan:

See: packages/react-native/types/__typetests__/index.tsx

@facebook-github-bot facebook-github-bot added CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Callstack Partner: Callstack Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Apr 10, 2024
@retyui retyui changed the title chore: [TS] Tranfrorm TouchableOpacity from class to ForwardRef component chore: [TS] Transform TouchableOpacity from class to ForwardRef component Apr 11, 2024
@retyui retyui force-pushed the chore/retyui/touchable-not-a-class branch from 581d814 to c55b7c0 Compare April 11, 2024 08:10
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@dmytrorykun 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 11, 2024
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@dmytrorykun merged this pull request in 3d00549.

@github-actions
Copy link
Copy Markdown

This pull request was successfully merged by @retyui in 3d00549.

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

@retyui retyui deleted the chore/retyui/touchable-not-a-class branch April 11, 2024 21:47
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. p: Callstack Partner: Callstack Partner 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.

2 participants