Thank you for your assistance. I have a question regarding the correct configuration of my specific scenario:
Currently, I have divided the code into three separate files:
(Note: The table named "events" is actually referred to as "eventi" and "table1" is called "locali")
-api/events/index.ts
import { supabase } from '@/lib/supabase';
import { useQuery } from '@tanstack/react-query';
export const useEventList = () => {
return useQuery({
queryKey: ["eventi"],
queryFn : async()=>{
const {data, error} = await supabase
.from("eventi")
.select(`*, locale:locali(*)`);
if(error)
throw new Error(error.message);
return data;
}
});
}
-My Home page (app/events/index.tsx)
import React, { useEffect, useState } from 'react';
import { View, Text, FlatList, ActivityIndicator } from 'react-native';
import EventListItem from "@/components/eventi/EventListItem";
import { useEventList } from '@/api/events';
export default function Home() {
const{data: eventi, error, isLoading} = useEventList();
if(isLoading){
return <ActivityIndicator />
}
if(error){
return <Text>Oops, something went wrong!</Text>
}
return (
<View style={{ flex: 1 }}>
<FlatList
style={{flex:1}}
data = {eventi}
renderItem={({item}) => <EventListItem evento={item}/>}
numColumns={1}
contentContainerStyle={{gap:10, padding:10}}
// columnWrapperStyle={{gap:10}}
/>
</View>
);
}
-EventListItem component
import { StyleSheet, View, Text, Pressable, Image, } from 'react-native';
import {Tables} from "@/types";
import { Link } from 'expo-router';
import { Colors } from '@/constants/Colors';
type EventListItemProps = {
evento: Tables<"eventi"> & {
locale: Tables<"locali">;
};
}
const EventListItem = ({evento}: EventListItemProps) => {
const dayjs = require('dayjs');
const eventDate = dayjs(evento.dt_inizio).format("DD/MM");
return(
<Link href={`/eventi/${evento.id}`} asChild>
<Pressable style={styles.card}>
{/* <Image
source = {{uri: evento.img || "" }}
/> */}
<Text>{evento.nome}</Text>
<Text>{evento.locale.nome}</Text>
<Text>{eventDate}</Text>
</Pressable>
</Link>
)
}
const styles = StyleSheet.create({
card: {
flex: 1,
borderRadius: 7,
paddingVertical: 5,
paddingHorizontal: 8,
backgroundColor: Colors.light.tabIconSelected,
}
})
export default EventListItem;
Any suggestions or recommendations are appreciated. As I am in the initial stages of my project, I am open to changing the structure and approach. Currently, I opted for this setup after exploring various tutorials as it appeared organized and concise.