Skip to content

Commit 7f2515e

Browse files
kaciebfacebook-github-bot
authored andcommitted
Add warning when scrollRef does not have a scrollTo method
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
1 parent d815be1 commit 7f2515e

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Libraries/Lists/VirtualizedList.js

+27
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,15 @@ class VirtualizedList extends React.PureComponent<Props, State> {
374374
return;
375375
}
376376

377+
if (this._scrollRef.scrollTo == null) {
378+
console.warn(
379+
'No scrollTo method provided. This may be because you have two nested ' +
380+
'VirtualizedLists with the same orientation, or because you are ' +
381+
'using a custom component that does not implement scrollTo.',
382+
);
383+
return;
384+
}
385+
377386
this._scrollRef.scrollTo(
378387
this.props.horizontal ? {x: offset, animated} : {y: offset, animated},
379388
);
@@ -437,6 +446,15 @@ class VirtualizedList extends React.PureComponent<Props, State> {
437446
return;
438447
}
439448

449+
if (this._scrollRef.scrollTo == null) {
450+
console.warn(
451+
'No scrollTo method provided. This may be because you have two nested ' +
452+
'VirtualizedLists with the same orientation, or because you are ' +
453+
'using a custom component that does not implement scrollTo.',
454+
);
455+
return;
456+
}
457+
440458
this._scrollRef.scrollTo(
441459
horizontal ? {x: offset, animated} : {y: offset, animated},
442460
);
@@ -478,6 +496,15 @@ class VirtualizedList extends React.PureComponent<Props, State> {
478496
return;
479497
}
480498

499+
if (this._scrollRef.scrollTo == null) {
500+
console.warn(
501+
'No scrollTo method provided. This may be because you have two nested ' +
502+
'VirtualizedLists with the same orientation, or because you are ' +
503+
'using a custom component that does not implement scrollTo.',
504+
);
505+
return;
506+
}
507+
481508
this._scrollRef.scrollTo(
482509
this.props.horizontal ? {x: offset, animated} : {y: offset, animated},
483510
);

0 commit comments

Comments
 (0)