Skip to content

fix: add missing getNativeScrollRef type for ScrollView#52203

Closed
zbauman3 wants to merge 1 commit intofacebook:mainfrom
zbauman3:improve-scrollview-types
Closed

fix: add missing getNativeScrollRef type for ScrollView#52203
zbauman3 wants to merge 1 commit intofacebook:mainfrom
zbauman3:improve-scrollview-types

Conversation

@zbauman3
Copy link
Copy Markdown
Contributor

@zbauman3 zbauman3 commented Jun 23, 2025

Summary:

The Problem

When trying to measure the location of a View within a ScrollView (ie. for scrolling to the view), the current recommended method is to use measureLayout on the nested view to determine its location inside the containing scroll view:

const MyComponent = () => {
  const scrollViewRef = useRef<ScrollView>(null);
  const nestedViewRef = useRef<View>(null);

  const scrollToNestedView = () => {
    if (!scrollViewRef.current || !nestedViewRef.current) {
      return;
    }

    nestedViewRef.current.measureLayout(
      scrollViewRef.current.getInnerViewNode(),
      (x, y) => { scrollViewRef.current.scrollTo({ y, animated: true }); },
    );
  }

  return (
    <ScrollView ref={scrollViewRef}>
      <View ref={nestedViewRef}>
        { /* content */ }
      </View>
    </ScrollView>
  );
}

This is valid in the Typescript types layer. However, the only two methods on ScrollView to use in this scenario that are available in the type definitions are getScrollableNode and getInnerViewNode – both of these methods return a number. The issue is that a number not a valid value to use with measureLayout because its source returns early for that type.

(Note, you can also use findNodeHandle with the scroll view ref, but this also returns a number.)

The Solution

The long-term solution would be to update the types for both measureLayout and ScrollView. However, that would constitute a breaking change and require some fairly expansive updates. Instead, I am proposing an additive solution.

ScrollView has a public method called getNativeScrollRef which returns the underlying HostInstance. This method correctly works in the runtime layer, but is not supported in the types layer. This PR exposes the public method in the type definition so that we can properly access the underlying instance without using @ts-ignore.

Changelog:[GENERAL] [FIXED] - Expose ScrollView.getNativeScrollRef on the type definition to allow accessing the underlying HostInstance.

Test Plan:

None needed. This is only a type update exposing existing functionality.

@facebook-github-bot
Copy link
Copy Markdown
Contributor

Hi @zbauman3!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@zbauman3 zbauman3 changed the title fix: add missing getNativeScrollRef type for ScrollView fix: add missing getNativeScrollRef type for ScrollView Jun 23, 2025
@facebook-github-bot
Copy link
Copy Markdown
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@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. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Jun 23, 2025
@zbauman3
Copy link
Copy Markdown
Contributor Author

@rshest or @cortinico, it looks like you two have done some previous reviewing on this file. I would love a review/feedback if either of you have time.

TYIA! 🙏

@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 Jun 23, 2025
@facebook-github-bot
Copy link
Copy Markdown
Contributor

@rshest merged this pull request in 4b91b63.

@react-native-bot
Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @zbauman3 in 4b91b63

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

huntie added a commit to huntie/react-native that referenced this pull request May 7, 2026
Summary:
Pull Request resolved: facebook#56718

Update the return type of `getNativeScrollRef` on `ScrollView`, `FlatList`, `SectionList` to be `PublicScrollViewInstance` (previously, a mix of `HostInstance` and `React.ElementRef<>` types which did not include the imperative methods of `ScrollView`).

- This type extends `HostInstance & ScrollViewImperativeMethods`, and is what the `ScrollView` ref chain already returns at runtime.
- The previous types were either too broad (`HostInstance`), wrong (union with `View`), or required `$FlowFixMe` suppressions.

Also removes `ScrollViewNativeComponent` from the public API surface, since it is an internal implementation detail not intended for external use (equivalent props are on the pre-existing `ScrollViewBaseProps` type).

Related to:

- facebook#52203
- facebook#54735

Changelog:
[General][Fixed] - **Strict TypeScript API**: Update `getNativeScrollRef` return type across ScrollView, FlatList, and SectionList

Differential Revision: D104223704
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.

3 participants