Skip to content

Instantly share code, notes, and snippets.

@akulsr0
Created October 30, 2020 05:27
Show Gist options
  • Save akulsr0/547b7b5a2333e97f5c31e2374012a2c8 to your computer and use it in GitHub Desktop.
Save akulsr0/547b7b5a2333e97f5c31e2374012a2c8 to your computer and use it in GitHub Desktop.
React Native UI - Contacts Screen
import React, { useState, useEffect } from 'react';
import {
ActivityIndicator,
StatusBar,
Image,
Text,
TouchableOpacity,
Alert,
View,
} from 'react-native';
import Axios from 'axios';
import AlphabetFlatList from '@yoonzm/react-native-alphabet-flat-list';
import { Feather as Icon } from '@expo/vector-icons';
function UserCard({ item }) {
return (
<View
style={{
margin: 4,
backgroundColor: '#fff',
marginRight: 40,
paddingHorizontal: 10,
paddingVertical: 4,
borderRadius: 10,
flexDirection: 'row',
alignItems: 'center',
height: 60,
}}
>
<View>
<Image
style={{ width: 50, height: 50, borderRadius: 100 }}
source={{ uri: item.picture.thumbnail }}
/>
</View>
<View style={{ flex: 1, paddingHorizontal: 10 }}>
<Text
style={{ fontSize: 16 }}
>{`${item.name.first} ${item.name.last}`}</Text>
</View>
<View style={{ flexDirection: 'row' }}>
<TouchableOpacity
onPress={() => {
Alert.alert(`Calling ${item.phone}`);
}}
>
<Icon name='phone' style={{ marginLeft: 12 }} size={20} />
</TouchableOpacity>
<TouchableOpacity
onPress={() => {
Alert.alert(`Messaging ${item.phone}`);
}}
>
<Icon name='message-circle' style={{ marginLeft: 14 }} size={20} />
</TouchableOpacity>
</View>
</View>
);
}
export default function AlphabetScrollScreen({ navigation }) {
StatusBar.setBarStyle('dark-content');
navigation.setOptions({
title: 'Contacts',
headerLeft: () => null,
});
const [users, setUsers] = useState(null);
useEffect(() => {
Axios.get('https://randomuser.me/api/?results=50').then(({ data }) => {
const alphabeticUsers = {
A: [],
B: [],
C: [],
D: [],
E: [],
F: [],
G: [],
H: [],
I: [],
J: [],
K: [],
l: [],
M: [],
N: [],
O: [],
P: [],
Q: [],
R: [],
S: [],
T: [],
U: [],
V: [],
W: [],
X: [],
Y: [],
Z: [],
};
data.results.map((user) => {
let fchar = user.name.first[0];
if (fchar in alphabeticUsers) {
alphabeticUsers[fchar] = [...alphabeticUsers[fchar], user];
}
});
setUsers(alphabeticUsers);
});
}, []);
if (users === null) {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<ActivityIndicator size='large' />
</View>
);
}
return (
<View style={{ flex: 1, paddingVertical: 10 }}>
<AlphabetFlatList data={users} itemHeight={60} renderItem={UserCard} />
</View>
);
}
@kiranmonangi
Copy link

it's not working...
I am facing the following error :

E:/EmployeeDirectory/node_modules/@yoonzm/react-native-alphabet-flat-list/built/AlphabetFlatList.js 88:20
Module parse failed: Unexpected token (88:20)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| });
| this.renderItem = (info) => {

        return (<react_native_1.View key={info.index}>

| {this.props.renderSectionHeader && this.props.renderSectionHeader({ title: info.item })}
| {this.props.data[info.item].map((itemValue, itemIndex, items) => this.props.renderItem({

@kiranmonangi
Copy link

Please give me a suggestion

@KartikDorbi99
Copy link

npm i react-native-flatlist-alphabet
run this in terminal and restart

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