Thuta Learning
IntermediateMobile Developmentintermediate

Fetching Data with API

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand Fetching Data with API without the intimidation factor
  • Write the code yourself and run it on Expo Go
  • Apply this concept immediately in a real app project

Let's think about it this way for a second

In React Native, the fetch API is available with the same syntax as in a web browser (it's built in) — if you want the API call to run only once, when the component mounts, put it inside useEffect(() => { ... }, []) (an empty dependency array). Combine two useState calls — a loading state (to show while waiting for data) and an error state (to show on failure) — and you get a complete data-fetching pattern.

Let's connect this to a real-world scenario

Wrapping the API call in try/catch matters for error handling — when there's no network connection (mobile users lose signal all the time), fetch can throw, and without a catch the app can crash. Show the ActivityIndicator component (a built-in loading spinner) while the loading state is true, and render the actual content once the data has arrived.

Code Example

javascript
import { useState, useEffect } from 'react';
import { View, Text, ActivityIndicator, FlatList } from 'react-native';

export default function PostsList() {
  const [posts, setPosts] = useState([]);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState(null);

  useEffect(() => {
    fetch('https://jsonplaceholder.typicode.com/posts')
      .then((res) => res.json())
      .then((data) => setPosts(data))
      .catch((err) => setError(err.message))
      .finally(() => setLoading(false));
  }, []);

  if (loading) return <ActivityIndicator size="large" />;
  if (error) return <Text>Error: {error}</Text>;

  return (
    <FlatList
      data={posts}
      keyExtractor={(item) => item.id.toString()}
      renderItem={({ item }) => <Text>{item.title}</Text>}
    />
  );
}
You should see
When the app opens, a loading spinner appears, and once the data arrives, the list of post titles is displayed in a FlatList.

Try it in 5 minutes

Fetch data from a public API (e.g. jsonplaceholder.typicode.com) and build a screen yourself that handles both the loading and error states.

A quick word of caution

Don't hardcode API keys or sensitive tokens in client code (remember the API Key Management lesson from the Cybersecurity tutorial) — decompiling a mobile app can expose hardcoded secrets.

Easy traps

  • Leaving out useEffect's dependency array ([]) so the API call runs over and over on every render
  • Running a fetch call without try/catch, so a network error crashes the app

Now try it yourself

Fetch data from a public API (e.g. jsonplaceholder.typicode.com) and build a screen yourself that handles both the loading and error states.

You'll know it worked when: When the app opens, a loading spinner appears, and once the data arrives, the list of post titles is displayed in a FlatList.

Fetching Data with API | Thuta Learning