Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save joshjhargreaves/b8eb67d24ce58a6d8bffb469f7eeaf39 to your computer and use it in GitHub Desktop.
Save joshjhargreaves/b8eb67d24ce58a6d8bffb469f7eeaf39 to your computer and use it in GitHub Desktop.
// Copy of Exponent snack example
// https://snack.expo.io/HJcgiI8kb
import React, { Component } from 'react';
import { Text, View, FlatList, Dimensions, Button, StyleSheet } from 'react-native';
const { width } = Dimensions.get('window');
const style = {
justifyContent: 'center',
alignItems: 'center',
width: width,
height: 50,
flex: 1,
borderWidth: 1,
};
const COLORS = ['deepskyblue','fuchsia', 'lightblue '];
function getData(number) {
let data = [];
for(var i=0; i<number; ++i)
{
data.push("" + i);
}
return data;
}
class ScrollToExample extends Component {
getItemLayout = (data, index) => (
{ length: 50, offset: 50 * index, index }
)
getColor(index) {
const mod = index%3;
return COLORS[mod];
}
scrollToIndex = () => {
let randomIndex = Math.floor(Math.random(Date.now()) * this.props.data.length);
this.flatListRef.scrollToIndex({animated: true, index: randomIndex});
}
scrollToItem = () => {
let randomIndex = Math.floor(Math.random(Date.now()) * this.props.data.length);
this.flatListRef.scrollToIndex({animated: true, index: "" + randomIndex});
}
render() {
return (
<View style={styles.container}>
<View style={styles.header}>
<Button
onPress={this.scrollToIndex}
title="Tap to scrollToIndex"
color="darkblue"
/>
<Button
onPress={this.scrollToItem}
title="Tap to scrollToItem"
color="purple"
/>
</View>
<FlatList
style={{ flex: 1 }}
ref={(ref) => { this.flatListRef = ref; }}
keyExtractor={item => item}
getItemLayout={this.getItemLayout}
initialScrollIndex={50}
initialNumToRender={2}
renderItem={({ item, index}) => (
<View style={{...style, backgroundColor: this.getColor(index)}}>
<Text>{item}</Text>
</View>
)}
{...this.props}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1
},
header: {
paddingTop: 20,
backgroundColor: 'darkturquoise',
alignItems: 'center',
justifyContent: 'center'
}
});
export default class app extends Component {
render() {
return <ScrollToExample
data={getData(100)}
/>
}
}
@tuan23
Copy link

tuan23 commented Oct 16, 2017

Hi,

Could you please give more details of how getItemLayout works? I have a FlatList with variable height row items, so not sure how to compute the getItemLayout method.

Thanks!

@Norfeldt
Copy link

The scrollToItem method uses a scrollToIndex call. Is this correct?

@rcerrejon
Copy link

The scrollToItem method uses a scrollToIndex call. Is this correct?

Correct, there´s no ScrollToItem method, so, no difference at all, both uses ScrollToIndex :-/

@sudarshan14
Copy link

There is a difference between ScrollToItem [We are passing item here] & ScrollToIndex[We are passing index here]. It is mentioned in the official documentation for flatlist.

@creativemind1
Copy link

There is a difference between ScrollToItem [We are passing item here] & ScrollToIndex[We are passing index here]. It is mentioned in the official documentation for flatlist.

He is telling about the above example. In the above example, both are using scrollToIndex

@AVert
Copy link

AVert commented Aug 9, 2020

This is not illustrate how to use scrollToItem !!!

@Joe1220
Copy link

Joe1220 commented Aug 21, 2020

how to use scrollToItem in FlatList component, not Button

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