-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.tsx
59 lines (53 loc) · 1.74 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import * as React from 'react';
import { FlatList, RefreshControl, StyleSheet, View } from 'react-native';
import { Stack, useNavigation } from 'expo-router';
import { useQuery } from '@tanstack/react-query';
import { Divider } from '@/react-query/components/Divider';
import { ListItem } from '@/react-query/components/ListItem';
import { useRefreshByUser } from '@/react-query/hooks/useRefreshByUser';
import { useRefreshOnFocus } from '@/react-query/hooks/useRefreshOnFocus';
import { fetchMovies, Movie } from '@/react-query/lib/api';
export default function MoviesListScreen() {
const navigation = useNavigation();
const { isPending, error, data, refetch } = useQuery<Movie[], Error>({
queryKey: ['movies'],
queryFn: fetchMovies,
});
const { isRefetchingByUser, refetchByUser } = useRefreshByUser(refetch);
useRefreshOnFocus(refetch);
const onListItemPress = React.useCallback(
(movie: any) => {
console.log('movie:', movie);
// @ts-expect-error: untyped
navigation.navigate('[movie]', {
movie: movie.title,
});
},
[navigation]
);
const renderItem = React.useCallback(
({ item }: { item: Movie }) => {
return <ListItem item={item} onPress={onListItemPress} />;
},
[onListItemPress]
);
return (
<View style={styles.container}>
<Stack.Screen options={{ title: 'React Query Example' }} />
<FlatList
data={data}
renderItem={renderItem}
keyExtractor={(item) => item.title}
ItemSeparatorComponent={() => <Divider />}
refreshControl={
<RefreshControl refreshing={isRefetchingByUser} onRefresh={refetchByUser} />
}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});