I have a typescript function that is responsible for fetching data from the NASA api using the React Native fetch function and returning a custom object that has type Item[]. The issue is that the returned object from this function is always [].The responseData.collection.items worked when originally setting a state variable of type Item[] to it so it is not a mapping issue.
However, I would like this to be a separate typescript function outside of any React components so I cannot use state.
What is the correct way of accessing the fetched JSON response data in typescript without using React Native state?
import { Item } from "../types/data"export function fetchData(searchTerm: string): Item[] {let result: Item[] = []fetch(`https://images-api.nasa.gov/search? q=${searchTerm}&media_type=image`) .then(response => response.json()) .then((responseData) => { result = responseData.collection.items }) .catch((error) => { console.error(error); }) return result }