Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6214

Accessing fetched data in React Native without using React State

$
0
0

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 }

Viewing all articles
Browse latest Browse all 6214

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>