From API to UI: Using React-query to Fetch and Display Data in React Native and React

9/4/20242 min read

Introduction

In modern mobile development, data management is a critical aspect that can greatly influence the performance and user experience of an app. For React Native developers, handling data fetching, caching, synchronization, and error handling can be challenging without the right tools. This is where useQuery from React Query comes into play. useQuery is a powerful hook that simplifies data fetching, caching, and updating in your React Native applications. It abstracts away much of the boilerplate code associated with data management and provides a clean, declarative API for interacting with remote data sources.

In this article, we’ll explore how to integrate React Query in a React Native app, focusing on a real estate application where we need to fetch a list of properties from an API. We'll utilize the container-presentational components approach to maintain a clean separation of concerns, and we'll use TypeScript for type safety and better developer experience

How to use React Query

step 1: Install React Query

check the official installation guide here

step 2: Configure the Query Client

Now, let’s set up the QueryClient at the root of your app to handle all your data fetching needs

import React from 'react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { NavigationContainer } from '@react-navigation/native';
import MainNavigator from './navigation/MainNavigator';

const queryClient = new QueryClient();

const App = () => {
return (
<QueryClientProvider client={queryClient}>
<NavigationContainer>
<MainNavigator />
</NavigationContainer>
</QueryClientProvider>
);
};

export default App;

step 3: Creating the Container Component

in this approach, we are going to use Container/Presentational Pattern to handle the data, check the full article about different react and react native patterns here.

  1. we will create first the fetch API in our ‘’api/properties’’

  2. We put the fetch properties inside our useQuery to get the data, and use it inside the Property Container Components, to handle different state such as error, loading and having the data

  3. We pass the data to the Presentational Components to show the data.

The following Code to explain all the approach

Conclusion

Using react query in a React Native app simplifies data fetching, error handling, and caching, making it easier to maintain and scale your application. By following the container-presentational pattern and leveraging TypeScript for type safety, you can build robust and efficient apps with clean and maintainable code. The approach demonstrated in this article can be easily adapted to other parts of your app, ensuring a consistent and scalable architecture

If you need to integrate Advanced functionalities in your Mobile app, create one from scratch, or need consulting in react native. contact us here and let us do the rest

#ReactNative #WebSockets #RealTime #MobileDevelopment #AppDevelopment #TechInnovation #Coding #JavaScript #MobileApps #DevCommunity #SocketProgramming #RealTimeCommunication #TechNavy