Skip to content

fix(button): type touchableRef as a ref to the touchable's instance - #5256

Open
isaacrowntree wants to merge 1 commit into
callstack:5.0from
isaacrowntree:fix/button-touchable-ref-type
Open

isaacrowntree wants to merge 1 commit into
callstack:5.0from
isaacrowntree:fix/button-touchable-ref-type

Conversation

@isaacrowntree

Copy link
Copy Markdown

Motivation

On React Native 0.87 and later, View in React Native's types is the component, not its instance (#5166). touchableRef?: React.RefObject<View> therefore can't take a ref to the touchable on 5.x:

  • useRef<ViewInstance>(null) (or useRef<React.ComponentRef<typeof View>>(null)) is rejected, so apps cast to pass it.
  • Callback refs aren't accepted on any React Native version, because RefObject excludes them.

#5167 fixed this on main by typing the prop React.Ref<React.ComponentRef<typeof View>>. This backports that typing to 5.x for Button's touchableRef.

On React Native versions before 0.87, React.ComponentRef<typeof View> is the View instance, so existing code still compiles; the prop now also accepts callback refs.

Related issue

Backport of the Button part of #5167, for #5166 ("5.x has the same problem").

The other refs #5167 retyped (Surface, IconButton, FAB, Card, …) have the same problem on 5.x. I kept this PR to Button and can backport the rest in a follow-up if you'd like.

Screenshots / Videos

None: types only, plus a test. No visual change.

Test plan

  1. New test: a callback touchableRef receives the touchable instance on mount and null on unmount. Before this change, the test doesn't compile (TS2741: Property 'current' is missing … RefObject<View>).
  2. yarn typescript and yarn test pass (930 tests). No snapshot changes. ESLint is clean on the changed files. yarn lint-no-fix reports one error that is already on 5.0 and unrelated to this change (import/order in example/src/index.native.tsx).
  3. Also type-checked in a React Native 0.88 app (default and react-native-strict-api conditions): const ref = useRef<ViewInstance>(null); <Button touchableRef={ref} /> compiles without a cast, and ref.current?.measure(…) is typed.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

Found potential problems with the pull request:

  • The description is too long. Please keep it under 1000 characters.
  • Screenshot or video evidence is missing. Make sure to include one if it affects the UI.

@Doberjohn Doberjohn added this to the 5.15.6 milestone Nov 20, 2026

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants