← Back to Chapters

React Native GET API

? React Native GET API

? Quick Overview

In React Native, a GET API request is used to fetch data from a server. The most common way is by using the built-in fetch() function to retrieve JSON data from REST APIs.

? Key Concepts

  • GET request fetches data without modifying server data
  • fetch() returns a Promise
  • Responses are usually in JSON format
  • Hooks like useEffect and useState are commonly used

? Syntax / Theory

The basic syntax for a GET request in React Native:

fetch(url).then(response => response.json())

? Code Example

? View Code Example
// Simple GET API example using fetch
import React, { useEffect, useState } from "react";
import { View, Text, FlatList } from "react-native";

const App = () => {
  const [data, setData] = useState([]);

  useEffect(() => {
    // 1. Fetch data from API
    fetch("https://jsonplaceholder.typicode.com/posts?_limit=5")
      .then(response => response.json())
      .then(json => setData(json))
      .catch(error => console.error(error));
  }, []);

  return (
    <View>
      <Text>Total Records: {data.length}</Text>
      
      {/* 2. Display Data */}
      <FlatList
        data={data}
        keyExtractor={({ id }) => id.toString()}
        renderItem={({ item }) => (
          <Text>{item.title}</Text>
        )}
      />
    </View>
  );
};

export default App;

? Live Output / Explanation

What Happens?

The app sends a GET request to the API URL. The server responds with JSON data, which is stored in state and displayed in the UI.

? Interactive Example

This simulator mimics a mobile screen. Click the button below to execute a real fetch() request to a public API and render the results, just like React Native.

My API App
No Data Loaded

Status: Idle

?️ Use Cases

  • Fetching user profiles
  • Loading product lists
  • Displaying news articles
  • Consuming public REST APIs

? Tips & Best Practices

  • Always handle errors using .catch()
  • Use loading indicators for better UX
  • Keep API URLs in constants
  • Use async/await for cleaner code

? Try It Yourself

  • Fetch data from another public API
  • Display API data using FlatList
  • Add a loading spinner
  • Handle API errors gracefully