← Back to Chapters

Activity Indicator in React Native

⏳ Activity Indicator in React Native

? Quick Overview

ActivityIndicator is a built-in React Native component used to display a loading spinner. It visually indicates that an operation such as data fetching or processing is in progress.

? Key Concepts

  • Used to show loading or waiting states
  • Available on both Android and iOS
  • Highly customizable (size, color, visibility)
  • Controlled using boolean values

? Syntax / Theory

The ActivityIndicator component is imported from react-native and supports multiple props.

  • animating → Shows or hides the loader (boolean)
  • sizesmall or large
  • color → Spinner color
? View Code Example
// Import ActivityIndicator and core components
import React from "react";
import { View, ActivityIndicator, StyleSheet } from "react-native";

const App = () => {
  return (
    <View style={styles.container}>
      <ActivityIndicator size="large" color="#2563eb" />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: "center",
    alignItems: "center"
  }
});

export default App;

?️ Live Output / Explanation

What happens?

A spinning loader appears at the center of the screen, indicating that the app is performing a background task. The spinner continues until the component is removed or animating is set to false.

?️ Interactive Example

In real applications, ActivityIndicator is often toggled using state. The simulator below mimics a React Native app fetching data. Click "Load Data" to see the ActivityIndicator in action.

 
Press the button below to simulate an API call.

(In React Native code, this corresponds to setting animating={true})

? View Logic Used in Simulation
// ActivityIndicator controlled using state
import React, { useEffect, useState } from "react";
import { View, ActivityIndicator, Text } from "react-native";

const App = () => {
  const [loading, setLoading] = useState(false);
  const [data, setData] = useState(null);

  const loadData = () => {
    setLoading(true); // Show Spinner
    
    // Simulate API delay
    setTimeout(() => {
      setLoading(false); // Hide Spinner
      setData("✅ Data Loaded Successfully!");
    }, 2000);
  };

  return (
    <View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
      {loading ? (
        <ActivityIndicator size="large" color="#2563eb" />
      ) : (
        <Text>{data || "Press button to load"}</Text>
      )}
    </View>
  );
};

export default App;

? Use Cases

  • Fetching API data
  • Submitting forms
  • Loading screens
  • Background computations

✅ Tips & Best Practices

  • Always pair loaders with meaningful user feedback
  • Hide the loader once data is available
  • Avoid blocking UI unnecessarily
  • Match spinner color with your app theme

? Try It Yourself

  • Change spinner size and color
  • Show loader during API calls
  • Wrap ActivityIndicator in a modal
  • Create a custom loading screen