Created
October 14, 2018 20:35
-
-
Save AlexBrasileiro/57c61286ce8b924dce4d5623c5c8210c to your computer and use it in GitHub Desktop.
React Native | ScrollTo
This file contains 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, Fragment } from 'react'; | |
import { StyleSheet, Text, View, ScrollView, TouchableOpacity, Dimensions } from 'react-native'; | |
const Menu = (props) => { | |
let itens = ['blue', 'yellow', 'green', 'orange', 'pink']; | |
const { menu } = styles; | |
return ( | |
<View style={menu}> | |
{ | |
itens.map(item => | |
<TouchableOpacity key={item.toLowerCase()} onPress={() => props.scrollToContent(item)}> | |
<Text style={{ color: '#fff', fontWeight: '900' }}>{item.toUpperCase()}</Text> | |
</TouchableOpacity> | |
) | |
} | |
</View> | |
) | |
} | |
export default class App extends Component { | |
scrollToContentFn(item) { | |
setTimeout(() => { | |
this.refs[item].measure((x, y, width, height) => { | |
this.scrollViewRef.scrollTo({ y, animated: true }) | |
}) | |
}) | |
} | |
render() { | |
const { container } = styles; | |
return ( | |
<Fragment> | |
<Menu scrollToContent={(item) => this.scrollToContentFn(item)} /> | |
<View style={container}> | |
<ScrollView ref={ref => this.scrollViewRef = ref}> | |
{ | |
(() => { | |
let i, max, elements = [], colors = ['blue', 'yellow', 'green', 'orange', 'pink']; | |
for (i = 0, max = 4; i <= max; i++) { | |
elements.push( | |
<View ref={colors[i]} key={i} style={{ backgroundColor: colors[i], padding: 20 }}> | |
<Text>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Voluptate, veniam recusandae! Odit aut fugit harum facilis excepturi. Perferendis, incidunt vero quos voluptas cupiditate minima tenetur, modi in praesentium ea obcaecati dolorem corrupti illo, soluta ullam amet rerum suscipit non labore provident odio? Vero quasi hic voluptate atque corporis accusamus iste, nobis consequuntur et! Nulla quisquam voluptatibus praesentium minus aut, inventore repellat ex consectetur molestias, at perferendis enim rem amet maiores temporibus esse! Facere porro voluptate earum laboriosam. Aperiam, odio? Minima fugiat nihil illo assumenda animi tempora architecto porro saepe excepturi dolor sit consequuntur expedita ex ipsam accusantium, cum distinctio nostrum?</Text> | |
</View> | |
) | |
} | |
return elements; | |
})() | |
} | |
</ScrollView> | |
</View> | |
</Fragment> | |
); | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
backgroundColor: '#F5FCFF', | |
height: (Dimensions.get('window').height - 50), | |
}, | |
menu: { | |
flexDirection: 'row', | |
justifyContent: 'space-around', | |
backgroundColor: '#000', | |
height: 50, | |
alignItems: 'center' | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment