Skip to content

Instantly share code, notes, and snippets.

@conrad-vanl
Last active April 28, 2017 08:53
Show Gist options
  • Save conrad-vanl/41e2abb244d0b6e1bd952bd4ff4b3cd7 to your computer and use it in GitHub Desktop.
Save conrad-vanl/41e2abb244d0b6e1bd952bd4ff4b3cd7 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import { View, Text, FlatList } from 'react-native';
const style = {
justifyContent: 'center',
alignItems: 'center',
height: 100,
margin: 25,
borderWidth: 1,
borderColor: 'black',
};
const Card = props => (
<View style={style}>
{props.children}
</View>
);
const longList = (new Array(100)).fill('').map((v, i) => `${i}`);
class ScrollToExample extends Component {
componentDidMount() {
this.list.scrollToIndex({ index: this.props.scrollToIndex || 0 });
}
getItemLayout = (data, index) => (
{ length: 150, offset: 150 * index, index }
);
render() {
return (
<FlatList
onScroll={(e) => { console.log('onScroll', e.nativeEvent); }}
style={{ flex: 1 }}
ref={(ref) => { this.list = ref; }}
{...this.props}
keyExtractor={item => item}
getItemLayout={this.getItemLayout}
renderItem={({ item }) => (
<Card><Text>{item}</Text></Card>
)}
/>
);
}
}
export default function() {
return (
<ScrollToExample
data={longList}
scrollToIndex={50}
/>
);
}
@leonidkuznetsov18
Copy link

leonidkuznetsov18 commented Apr 28, 2017

Props data in ScrollToExample you take in function getItemLayout ?

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