Skip to content

Commit d1bed5a

Browse files
committed
fix: optimize flatlist
1 parent a2581a8 commit d1bed5a

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

sample-apps/react-native/ExpoTikTokApp/components/PlaceSearchDropdown.tsx

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect, useRef } from 'react';
1+
import React, { useState, useEffect, useRef, useCallback } from 'react';
22
import {
33
TextInput,
44
Text,
@@ -22,6 +22,18 @@ export type Place = {
2222
id: string;
2323
};
2424

25+
const ListEmptyComponent = () => (
26+
<View style={styles.emptyContainer}>
27+
<Ionicons name="search-outline" size={40} color="#ccc" />
28+
<Text style={styles.title}>No locations found</Text>
29+
<Text style={styles.subtitle}>
30+
Try searching for a place, hotel or attraction
31+
</Text>
32+
</View>
33+
);
34+
35+
const keyExtractor = (item: Place) => item.id;
36+
2537
export const PlaceSearchDropdown = () => {
2638
const [query, setQuery] = useState('');
2739
const [selectedLocation, setSelectedLocation] = useState<string | null>(null);
@@ -50,10 +62,18 @@ export const PlaceSearchDropdown = () => {
5062
country: string;
5163
place_id: string;
5264
address_line1: string;
53-
address_line2: string
65+
address_line2: string;
5466
};
5567
}) => {
56-
const { lat, lon, city, country, place_id, address_line1, address_line2 } = place.properties;
68+
const {
69+
lat,
70+
lon,
71+
city,
72+
country,
73+
place_id,
74+
address_line1,
75+
address_line2,
76+
} = place.properties;
5777

5878
return {
5979
address: `${address_line1}, ${address_line2}`,
@@ -105,6 +125,21 @@ export const PlaceSearchDropdown = () => {
105125
router.back();
106126
});
107127

128+
const renderItem = useCallback(
129+
({ item }: { item: Place }) => (
130+
<Pressable
131+
onTouchStart={() => {
132+
resultPressedRef.current = true;
133+
}}
134+
style={styles.item}
135+
onPress={() => handleSelect(item)}
136+
>
137+
<Text style={styles.name}>{item.name}</Text>
138+
</Pressable>
139+
),
140+
[handleSelect],
141+
);
142+
108143
return (
109144
<View style={styles.container}>
110145
<View style={styles.header}>
@@ -136,27 +171,9 @@ export const PlaceSearchDropdown = () => {
136171
data={results}
137172
contentContainerStyle={styles.listContainer}
138173
style={{ flex: 1 }}
139-
renderItem={({ item }: { item: Place }) => (
140-
<Pressable
141-
onTouchStart={() => {
142-
resultPressedRef.current = true;
143-
}}
144-
style={styles.item}
145-
onPress={() => handleSelect(item)}
146-
>
147-
<Text style={styles.name}>{item.name}</Text>
148-
</Pressable>
149-
)}
150-
keyExtractor={(item: Place) => item.id}
151-
ListEmptyComponent={() => (
152-
<View style={styles.emptyContainer}>
153-
<Ionicons name="search-outline" size={40} color="#ccc" />
154-
<Text style={styles.title}>No locations found</Text>
155-
<Text style={styles.subtitle}>
156-
Try searching for a place, hotel or attraction
157-
</Text>
158-
</View>
159-
)}
174+
renderItem={renderItem}
175+
keyExtractor={keyExtractor}
176+
ListEmptyComponent={ListEmptyComponent}
160177
/>
161178
</View>
162179
</View>

0 commit comments

Comments
 (0)