Simplifying Asynchronous JavaScript: Implementing async/await in React Native

Khusni Ja'far
Tulisan Khusni
Published in
3 min readMay 2, 2023

React Native provides powerful hooks that allow developers to implement asynchronous functionality in functional components. One of the most popular hooks is the useEffect hook, which can be used to trigger an asynchronous operation when a component mounts or updates. In this article, we'll explore how to implement async/await in React Native using hooks.

Before we dive into how to implement async/await using hooks, let's take a quick look at what async/await is. Async/await is a newer feature in JavaScript that allows developers to write asynchronous code in a more synchronous style. It allows developers to write code that looks and behaves like synchronous code, making it easier to read and understand.

To use async/await in React Native using hooks, we can use the useEffect hook to trigger an asynchronous operation when a component mounts or updates. Let's take a look at an example:

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

const fetchUserData = async () => {
try {
const response = await fetch('https://jsonplaceholder.typicode.com/users');
const json = await response.json();
return json;
} catch (error) {
console.error(error);
}
};

const UserList = () => {
const [isLoading, setIsLoading] = useState(true);
const [users, setUsers] = useState([]);

useEffect(() => {
const fetchUsers = async () => {
const usersData = await fetchUserData();
setUsers(usersData);
setIsLoading(false);
};
fetchUsers();
}, []);

if (isLoading) {
return <ActivityIndicator />;
}

return (
<FlatList
data={users}
keyExtractor={(user) => user.id}
renderItem={({ item }) => (
<View>
<Text>{item.name}</Text>
<Text>{item.email}</Text>
</View>
)}
/>
);
};

export default UserList;

In the example above, we have created a functional component called UserList that fetches user data from a web API using the fetchUserData function. We are using the useState hook to manage the isLoading and users states.

We are then using the useEffect hook to trigger the fetchUsers function when the component mounts. We are passing an empty array as the second argument to useEffect, which ensures that the effect only runs once when the component mounts.

Inside the fetchUsers function, we are using the await keyword to pause execution until the fetchUserData function is complete and the response is returned. We are then setting the users state with the response data and setting isLoading to false.

To render the user data in the component, we are using the FlatList component provided by React Native. We are passing the users data to the data prop of the FlatList component and defining a keyExtractor function to extract the id from each user object. We are then defining a renderItem function to render each item in the FlatList.

In conclusion, implementing async/await in React Native using hooks is a powerful tool that allows developers to write asynchronous code in a more synchronous style. The useEffect hook is an essential tool that triggers asynchronous operations when a component mounts or updates. Developers should choose the method that is best suited for their needs based on the specific requirements of their application.

--

--