Here's an example of a React Native component that fetches data from an API and displays it using TypeScript.
index.ts1267 chars67 linesIn this example, we define a Props interface that accepts an apiUrl string. We then define a User interface for the expected data we'll fetch from the API.
We declare some state variables using the useState hook; users will hold the fetched data, and loading will handle whether or not we display a "Loading" message.
In the useEffect hook, we fetch data from the passed apiUrl prop using the fetch function. We then convert the response to JSON and set the users state with the response data. Finally, we set the loading state to false.
If loading is truthy, we render a "Loading" message. Otherwise, we map through the users state and display the name and email address of each user.
We also define some basic styles using the StyleSheet.create function.
This component can be used by passing in an API URL as the apiUrl prop.
gistlibby LogSnag