Skip to content

Commit

Permalink
fix crash with onEndReached={null}
Browse files Browse the repository at this point in the history
Reviewed By: bvaughn

Differential Revision: D4815310

fbshipit-source-id: 69d4a5a6fd247bdf877465a7cd07924a0dd6584b
  • Loading branch information
sahrens authored and facebook-github-bot committed Apr 4, 2017
1 parent e0bd35f commit b12f6db
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 12 deletions.
8 changes: 7 additions & 1 deletion Examples/UIExplorer/js/FlatListExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class FlatListExample extends React.PureComponent {
static description = 'Performant, scrollable list of data.';

state = {
data: genItemData(1000),
data: genItemData(100),
debug: false,
horizontal: false,
filterText: '',
Expand Down Expand Up @@ -142,6 +142,7 @@ class FlatListExample extends React.PureComponent {
}
legacyImplementation={false}
numColumns={1}
onEndReached={this._onEndReached}
onRefresh={this._onRefresh}
onScroll={this.state.horizontal ? this._scrollSinkX : this._scrollSinkY}
onViewableItemsChanged={this._onViewableItemsChanged}
Expand All @@ -157,6 +158,11 @@ class FlatListExample extends React.PureComponent {
_getItemLayout = (data: any, index: number) => {
return getItemLayout(data, index, this.state.horizontal);
};
_onEndReached = () => {
this.setState((state) => ({
data: state.data.concat(genItemData(100, state.data.length)),
}));
};
_onRefresh = () => alert('onRefresh: nothing to refresh :P');
_renderItemComponent = ({item}) => {
return (
Expand Down
4 changes: 2 additions & 2 deletions Examples/UIExplorer/js/ListExampleShared.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ const {

type Item = {title: string, text: string, key: number, pressed: boolean, noImage?: ?boolean};

function genItemData(count: number): Array<Item> {
function genItemData(count: number, start: number = 0): Array<Item> {
const dataBlob = [];
for (let ii = 0; ii < count; ii++) {
for (let ii = start; ii < count + start; ii++) {
const itemHash = Math.abs(hashCode('Item ' + ii));
dataBlob.push({
title: 'Item ' + ii,
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Lists/VirtualizedList.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,6 @@ class VirtualizedList extends React.PureComponent<OptionalProps, Props, State> {
return String(index);
},
maxToRenderPerBatch: 10,
onEndReached: () => {},
onEndReachedThreshold: 2, // multiples of length
removeClippedSubviews: true,
renderScrollComponent: (props: Props) => {
Expand Down Expand Up @@ -636,7 +635,8 @@ class VirtualizedList extends React.PureComponent<OptionalProps, Props, State> {
}
const distanceFromEnd = contentLength - visibleLength - offset;
const itemCount = getItemCount(data);
if (this.state.last === itemCount - 1 &&
if (onEndReached &&
this.state.last === itemCount - 1 &&
distanceFromEnd < onEndReachedThreshold * visibleLength &&
(this._hasDataChangedSinceEndReached ||
this._scrollMetrics.contentLength !== this._sentEndForContentLength)) {
Expand Down
4 changes: 0 additions & 4 deletions Libraries/Lists/__tests__/__snapshots__/FlatList-test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ exports[`FlatList renders all the bells and whistles 1`] = `
maxToRenderPerBatch={10}
numColumns={2}
onContentSizeChange={[Function]}
onEndReached={[Function]}
onEndReachedThreshold={2}
onLayout={[Function]}
onRefresh={[Function]}
Expand Down Expand Up @@ -134,7 +133,6 @@ exports[`FlatList renders empty list 1`] = `
maxToRenderPerBatch={10}
numColumns={1}
onContentSizeChange={[Function]}
onEndReached={[Function]}
onEndReachedThreshold={2}
onLayout={[Function]}
onScroll={[Function]}
Expand Down Expand Up @@ -164,7 +162,6 @@ exports[`FlatList renders null list 1`] = `
maxToRenderPerBatch={10}
numColumns={1}
onContentSizeChange={[Function]}
onEndReached={[Function]}
onEndReachedThreshold={2}
onLayout={[Function]}
onScroll={[Function]}
Expand Down Expand Up @@ -206,7 +203,6 @@ exports[`FlatList renders simple list 1`] = `
maxToRenderPerBatch={10}
numColumns={1}
onContentSizeChange={[Function]}
onEndReached={[Function]}
onEndReachedThreshold={2}
onLayout={[Function]}
onScroll={[Function]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ exports[`SectionList rendering empty section headers is fine 1`] = `
keyExtractor={[Function]}
maxToRenderPerBatch={10}
onContentSizeChange={[Function]}
onEndReached={[Function]}
onEndReachedThreshold={2}
onLayout={[Function]}
onScroll={[Function]}
Expand Down Expand Up @@ -130,7 +129,6 @@ exports[`SectionList renders all the bells and whistles 1`] = `
keyExtractor={[Function]}
maxToRenderPerBatch={10}
onContentSizeChange={[Function]}
onEndReached={[Function]}
onEndReachedThreshold={2}
onLayout={[Function]}
onRefresh={[Function]}
Expand Down Expand Up @@ -269,7 +267,6 @@ exports[`SectionList renders empty list 1`] = `
keyExtractor={[Function]}
maxToRenderPerBatch={10}
onContentSizeChange={[Function]}
onEndReached={[Function]}
onEndReachedThreshold={2}
onLayout={[Function]}
onScroll={[Function]}
Expand Down

0 comments on commit b12f6db

Please sign in to comment.