Logo

dev-resources.site

for different kinds of informations.

Enhance Your React Native App with Debouncing for API Optimization

Published at
1/12/2025
Categories
reactnative
android
ios
debounce
Author
amitkumar13
Categories
4 categories in total
reactnative
open
android
open
ios
open
debounce
open
Author
11 person written this
amitkumar13
open
Enhance Your React Native App with Debouncing for API Optimization

Debouncing is a powerful technique used to optimize performance by limiting the number of times a function is executed. In React Native, debouncing is especially useful for scenarios like search inputs, where API calls need to be minimized to avoid overloading the server or causing unnecessary delays.

In this blog, Iโ€™ll walk through the process of building a React Native component that fetches data and utilizes debouncing to handle user input efficiently.


What is Debouncing?

Debouncing delays the execution of a function until after a specified delay has passed since the last time the function was invoked. If the function is triggered again before the delay ends, the timer resets. This technique is particularly useful in cases like:

  • Search inputs
  • Auto-save features
  • Scroll events

By using debouncing, we ensure the function (e.g., an API call) executes only after the user has finished their action, such as typing in a search box.


Debouncing in Action

To fully understand the benefits of debouncing, letโ€™s take a look at the difference between the app with debouncing and without debouncing.

Without Debouncing

In this scenario, every keystroke triggers an API call, resulting in multiple requests being sent in quick succession. This could lead to excessive load on the server and cause performance issues.

Image description

With Debouncing

With debouncing enabled, the app waits until the user pauses typing for a predefined period (in this case, 1 second) before making the API call. This minimizes unnecessary requests, making the app much more efficient.

Image description


Project Overview

In this project, we'll build a component that:

  1. Fetches data from Reddit's r/pics subreddit.
  2. Displays a list of posts.
  3. Provides a search bar for users to filter posts using debounced API calls.

Implementation

Here's how we built the component:

1. Setting Up State and API Fetching
We use the useState hook to manage the state of posts, the loading status, and the search term. The fetchRedditData function retrieves posts from the Reddit API based on the search query.

2. Adding Debouncing
We use the debounce utility from lodash to delay the API call until the user has stopped typing for a specific duration. This minimizes unnecessary API calls and improves the user experience.

3. Rendering the Component
The posts are displayed in a FlatList, while a TextInput allows users to type their search query.


Code

Below is the full implementation:

import React, { useEffect, useState, useCallback } from 'react';
import {
  View,
  Text,
  FlatList,
  Image,
  ActivityIndicator,
  StyleSheet,
  TextInput,
} from 'react-native';
import { debounce } from 'lodash';

const DebouncingComponent = () => {
  const [posts, setPosts] = useState([]);
  const [loading, setLoading] = useState(false);
  const [searchTerm, setSearchTerm] = useState('');

  // Fetch data from Reddit API
  const fetchRedditData = async (query) => {
    try {
      setLoading(true);
      console.log('Fetching data for query:', query);
      const url = query
        ? `https://api.reddit.com/r/pics/search.json?q=${query}&restrict_sr=1`
        : `https://api.reddit.com/r/pics/hot.json`;

      const response = await fetch(url);
      const json = await response.json();
      const fetchedPosts = json.data.children.map((item) => item.data);
      console.log('API Call Successful'); // Debugging output
      setPosts(fetchedPosts);
    } catch (error) {
      console.error('Error fetching Reddit data:', error);
    } finally {
      setLoading(false);
    }
  };

  // Debounced fetch function
  const debouncedFetch = useCallback(debounce(fetchRedditData, 1000), []);

  // Handle search input changes
  const handleSearchChange = (text) => {
    setSearchTerm(text);
    debouncedFetch(text); // Trigger the debounced fetch
  };

  // Fetch initial data on component mount
  useEffect(() => {
    fetchRedditData();
  }, []);

  // Render a single post
  const renderPost = ({ item }) => (
    <View style={styles.postContainer}>
      <Text style={styles.title}>{item.title}</Text>
      {item.thumbnail && item.thumbnail.startsWith('http') ? (
        <Image source={{ uri: item.thumbnail }} style={styles.thumbnail} />
      ) : null}
      <Text style={styles.author}>By: {item.author}</Text>
    </View>
  );

  return (
    <View style={styles.container}>
      <TextInput
        style={styles.searchInput}
        placeholder="Search posts..."
        value={searchTerm}
        onChangeText={handleSearchChange}
      />
      {loading ? (
        <View style={styles.loadingContainer}>
          <ActivityIndicator size="large" color="#0000ff" />
        </View>
      ) : (
        <FlatList
          data={posts}
          keyExtractor={(item) => item.id}
          renderItem={renderPost}
          contentContainerStyle={styles.listContainer}
        />
      )}
    </View>
  );
};

export default DebouncingComponent;

// Styles
const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 10,
  },
  loadingContainer: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  listContainer: {
    paddingBottom: 10,
  },
  searchInput: {
    height: 40,
    borderColor: 'gray',
    borderWidth: 1,
    borderRadius: 5,
    paddingHorizontal: 10,
    marginBottom: 10,
  },
  postContainer: {
    marginBottom: 15,
    padding: 10,
    backgroundColor: '#f9f9f9',
    borderRadius: 8,
    borderWidth: 1,
    borderColor: '#ddd',
  },
  title: {
    fontSize: 16,
    fontWeight: 'bold',
    marginBottom: 5,
  },
  author: {
    fontSize: 12,
    color: '#555',
  },
  thumbnail: {
    height: 100,
    width: 100,
    marginTop: 10,
    borderRadius: 8,
  },
});

Enter fullscreen mode Exit fullscreen mode

Key Features

  1. Efficient API Calls: The debounce utility ensures that the API is called only after the user stops typing, reducing redundant requests.
  2. Real-Time Feedback: A loading spinner (ActivityIndicator) is displayed while data is being fetched.
  3. Dynamic Search: Users can dynamically search for posts within r/pics.

Benefits of Using Debouncing

1. Performance Optimization: Reduces the number of API calls.
2. Improved UX: Prevents flickering or delays caused by excessive fetching.
3. Server Load Management: Helps in maintaining efficient server performance by minimizing redundant requests.

Conclusion

Debouncing is a simple yet effective tool for improving the performance and user experience of React Native applications. By combining useState, useCallback, and lodash's debounce, weโ€™ve created a robust solution for handling search inputs and fetching data from an API.

This pattern is widely applicable and can be extended to various scenarios like search bars, form validations, and more. Try incorporating debouncing into your next React Native project to see the difference! ๐Ÿš€

debounce Article's
30 articles in total
Favicon
If you think debounce or onblur is not working in React, you can check it.
Favicon
Enhance Your React Native App with Debouncing for API Optimization
Favicon
Dboun
Favicon
React: Debouncing input with useEffect
Favicon
Debounce Method for Searching
Favicon
Optimizing React Performance with useDebounce.
Favicon
Understanding JavaScript Debounce and Throttling
Favicon
Debounce function with a return value using promises
Favicon
Introducing dwayne/elm-debouncer: Debounce or throttle your actions in Elm
Favicon
Debouncing De-coded
Favicon
Some Problems I Face When Building React Single Page Application And How I Solve Them
Favicon
Debounce Input in React
Favicon
Como aplicar debounce em input de busca sem lodash
Favicon
Debouncing: how to use it
Favicon
Debounce in Next.js
Favicon
Optimized React Search Bar ๐Ÿ”ฏ
Favicon
Learn Debounce and Throttle
Favicon
ะ˜ัะฟะพะปัŒะทัƒะตะผ Throttle ะธ Debounce ะฒ React
Favicon
Debouncing in Javascript using a custom function or Lodash library.
Favicon
Debounce Function in Javascript ๐Ÿš€
Favicon
Debouncing TypeScript
Favicon
How to use Lodash debounce method?
Favicon
Debounce in JavaScript
Favicon
What is Debouncing? Search Suggestions - A Javascript Implementation
Favicon
Throttle and Debounce in React
Favicon
Debounce and Throttle
Favicon
useDebounce hook in React, Improve performance of your React app with Debouncing
Favicon
Debouncing (lodash) with React
Favicon
Use lodash.debounce inside a function component in React
Favicon
How to Debounce UIButton Taps

Featured ones: