From e7a0979b8f3ccd2334ea9114eb891334ef712e30 Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Fri, 28 Jun 2019 17:57:13 -0700 Subject: [PATCH] warn if VirtualizedList is embedded in a plain ScrollView Summary: This breaks virtualization, viewability callbacks, and other features, so should be warned against. Hopefully this would have made D15890785 trivial to figure out. Reviewed By: PeteTheHeat Differential Revision: D16040939 fbshipit-source-id: 593cd5da9891450fdcb501aef41455cf2d7baa4f --- Libraries/Components/ScrollView/ScrollView.js | 8 +++++++ Libraries/Lists/VirtualizedList.js | 23 ++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Libraries/Components/ScrollView/ScrollView.js b/Libraries/Components/ScrollView/ScrollView.js index 260bc0fd94c..ba00259648d 100644 --- a/Libraries/Components/ScrollView/ScrollView.js +++ b/Libraries/Components/ScrollView/ScrollView.js @@ -573,6 +573,10 @@ function createScrollResponder( return scrollResponder; } +type ContextType = {||} | null; +const Context = React.createContext(null); +const standardContext: ContextType = Object.freeze({}); // not null with option value to add more info in the future + /** * Component that wraps platform ScrollView while providing * integration with touch locking "responder" system. @@ -609,6 +613,7 @@ function createScrollResponder( * supports out of the box. */ class ScrollView extends React.Component { + static Context = Context; /** * Part 1: Removing ScrollResponder.Mixin: * @@ -999,6 +1004,9 @@ class ScrollView extends React.Component { } }); } + children = ( + {children} + ); const hasStickyHeaders = Array.isArray(stickyHeaderIndices) && stickyHeaderIndices.length > 0; diff --git a/Libraries/Lists/VirtualizedList.js b/Libraries/Lists/VirtualizedList.js index d060dbf9fa6..534e10783df 100644 --- a/Libraries/Lists/VirtualizedList.js +++ b/Libraries/Lists/VirtualizedList.js @@ -968,7 +968,7 @@ class VirtualizedList extends React.PureComponent { this._hasMore = this.state.last < this.props.getItemCount(this.props.data) - 1; - const ret = React.cloneElement( + const innerRet = React.cloneElement( (this.props.renderScrollComponent || this._defaultRenderScrollComponent)( scrollProps, ), @@ -977,6 +977,27 @@ class VirtualizedList extends React.PureComponent { }, cells, ); + let ret = innerRet; + if (__DEV__) { + ret = ( + + {scrollContext => { + if ( + scrollContext != null && + !this._hasWarned.nesting && + this.context.virtualizedList == null + ) { + // TODO (T46547044): use React.warn once 16.9 is sync'd: https://github.com/facebook/react/pull/15170 + console.warn( + 'VirtualizedLists should never be nested inside a plain ScrollView - use another VirtualizedList-backed container instead.', + ); + this._hasWarned.nesting = true; + } + return innerRet; + }} + + ); + } if (this.props.debug) { return (