Skip to content

Commit

Permalink
Add warning when scrollRef does not have a scrollTo method
Browse files Browse the repository at this point in the history
Summary:
Add a `console.warn()` call when calling `_scrollRef.scrollTo`, because `scrollTo` is not guaranteed to exist on `_scrollRef`.

Context:
`VirtualizedList` holds `_scrollRef`, which is usually a reference to a `ScrollView` component. However, there are several cases where it holds a `View` or other type of component instead.

A custom component can be passed in to `renderScrollComponent`, and then `_scrollRef` will point to that custom component. Additionally, if two VirtualizedLists are nested with the same orientation, `_defaultRenderScrollComponent` will return a View instead of a ScrollView.

Due to these possibilities, `_scrollRef` is not guaranteed to have a `scrollTo` method.

Changelog: [General] [Added] - Add warning when scrollRef does not have a scrollTo method

Reviewed By: JoshuaGross, TheSavior

Differential Revision: D21386842

fbshipit-source-id: 01e167e0ae0edea8f29853e8b242ce88a5103b49
  • Loading branch information
kacieb authored and facebook-github-bot committed May 4, 2020
1 parent d815be1 commit 7f2515e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Libraries/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,15 @@ class VirtualizedList extends React.PureComponent<Props, State> {
return;
}

if (this._scrollRef.scrollTo == null) {
console.warn(
'No scrollTo method provided. This may be because you have two nested ' +
'VirtualizedLists with the same orientation, or because you are ' +
'using a custom component that does not implement scrollTo.',
);
return;
}

this._scrollRef.scrollTo(
this.props.horizontal ? {x: offset, animated} : {y: offset, animated},
);
Expand Down Expand Up @@ -437,6 +446,15 @@ class VirtualizedList extends React.PureComponent<Props, State> {
return;
}

if (this._scrollRef.scrollTo == null) {
console.warn(
'No scrollTo method provided. This may be because you have two nested ' +
'VirtualizedLists with the same orientation, or because you are ' +
'using a custom component that does not implement scrollTo.',
);
return;
}

this._scrollRef.scrollTo(
horizontal ? {x: offset, animated} : {y: offset, animated},
);
Expand Down Expand Up @@ -478,6 +496,15 @@ class VirtualizedList extends React.PureComponent<Props, State> {
return;
}

if (this._scrollRef.scrollTo == null) {
console.warn(
'No scrollTo method provided. This may be because you have two nested ' +
'VirtualizedLists with the same orientation, or because you are ' +
'using a custom component that does not implement scrollTo.',
);
return;
}

this._scrollRef.scrollTo(
this.props.horizontal ? {x: offset, animated} : {y: offset, animated},
);
Expand Down

0 comments on commit 7f2515e

Please sign in to comment.