Last active
April 28, 2017 08:53
-
-
Save conrad-vanl/41e2abb244d0b6e1bd952bd4ff4b3cd7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | |
/> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Props
data
inScrollToExample
you take in functiongetItemLayout
?