Skip to content

Instantly share code, notes, and snippets.

@ranveerching
Last active October 15, 2020 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ranveerching/d57c08e94051ada92423221787d87241 to your computer and use it in GitHub Desktop.
Save ranveerching/d57c08e94051ada92423221787d87241 to your computer and use it in GitHub Desktop.
import React, { useRef, useEffect } from 'react';
import {
View,
Text,
FlatList,
StyleSheet
} from 'react-native';
import { useNavigation } from '@react-navigation/native';
import { listItems, scrollToTop } from '../services';
const renderItem = (data, itemBackgroundColor) => {
const { item } = data;
return (
<View style={[styles.itemContainer, {
backgroundColor: itemBackgroundColor
}]}>
<Text style={styles.itemText}>{item.title}</Text>
</View>
)
};
const itemSeparatorComponent = () => <View style={styles.separator} />;
const ListComponent = props => {
const {
itemBackgroundColor,
containerBackgroundColor
} = props;
const listRef = useRef(null);
const navigation = useNavigation();
useEffect(() => {
if (listRef?.current) {
scrollToTop(navigation, listRef);
}
}, []);
return (
<View style={[styles.container, {
backgroundColor: containerBackgroundColor
}]}>
<FlatList
keyExtractor={item => item.id.toString()}
contentContainerStyle={styles.contentContainerStyle}
ref={listRef}
data={listItems}
showsVerticalScrollIndicator={false}
ItemSeparatorComponent={itemSeparatorComponent}
renderItem={data => renderItem(data, itemBackgroundColor)}
/>
</View>
)
};
const styles = StyleSheet.create({
container: {
flex: 1,
},
contentContainerStyle: {
padding: 10
},
itemContainer: {
padding: 15,
borderRadius: 5
},
itemText: {
fontSize: 15,
textAlign: 'center',
color: 'white'
},
separator: {
height: 10
}
});
export default ListComponent;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment