export interface JobListing {
id: number,
isTraded: boolean,
feedback: string,
title: string,
message: string,
skills: string[],
sender: string,
ipAddress: string
}
const GroupList = () => {
const [jobListings, setJobListings] = useState<JobListing[]>([]);
useEffect(() => {
database.collection("job_listings").onSnapshot((snapshot: any) => (
setJobListings(snapshot.docs.map((doc: any) => doc.data()))
));
alert(jobListings[0].message)
return () => {
}
}, [])
return ('JSX code')
}
export default GroupList;
I have useEffect where I am setting my listings by useState hook from firebase. Everythink works fine when I am using map to iterate over jobListings and returning table cell for each one in JSX component. However whenever in code I want access jobListings[some_index].message I am getting Cannot read property 'message' of undefined. For example in alert in code. As you see I am using typescript and have type for jobListings. When I set up instead empty array some item with appropriate properties, I was able to access index 0 jobListings[0].message.