Skip to content

Instantly share code, notes, and snippets.

@viviancromwell
Last active June 16, 2017 13:03
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save viviancromwell/fe9714582270250e2989 to your computer and use it in GitHub Desktop.
Save viviancromwell/fe9714582270250e2989 to your computer and use it in GitHub Desktop.
Infinite List with React Native (OnRender: How to rerender the Infinite Scroll Widget)
updateRenderModel(contentOffset) {
var listItemHeight = ITEM_SIZE;
// Use contentOffset to calculate first visible dataItem as y-position / height of item
var firstVisibleItem = Math.max(0, Math.floor(contentOffset.y / listItemHeight));
var renderModelSize = BUFFER_ITEMS * 2 + DISPLAY_ITEMS;
// Calculate first y-position
var nextPosition = (firstVisibleItem - BUFFER_ITEMS) * listItemHeight;
// Subset of dataModel to be rendered.
var dataItems = this.state.dataModel.slice(firstVisibleItem, firstVisibleItem + renderModelSize);
var newRenderModel = dataItems.map((dataItem, index) => {
return {
key: dataItem.id,
data: dataItem.data,
position: nextPosition + index * listItemHeight
}
});
// update renderModel, as well as bodyHeight of scroll area to encompass the largest y value
var state = {
renderModel: newRenderModel,
dataModel : this.state.dataModel,
bodyHeight : nextPosition + renderModelSize * listItemHeight
};
this.setState(state);
}
@johuder33
Copy link

johuder33 commented Jan 10, 2017

Hi @viviancromwell, I did read your post, and it seemed to me really great, I was try to implement the things that you has written in that post, so I am a little confused, because the code above, you has declared two constants, these are BUFFER_ITEMS and DISPLAY_ITEMS, but I don't understand what you refer with theses variables. I really appreciate your answer. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment