Open In App

Fetching Data from an API with useEffect and useState Hook

In modern web development, integrating APIs to fetch data is a common task. In React applications, the useEffect and useState hooks provide powerful tools for managing asynchronous data fetching. Combining these hooks enables fetching data from APIs efficiently.

This article explores how to effectively utilize these hooks to fetch data from an API, focusing on the Random Data API for practical examples



useState Hook:

React useState Hook allows to store and access of the state in the functional components. State refers to data or properties that need to be stored in an application.

Syntax of useState Hook:

const [state, setState] = useState(initialState);

useEffect Hook:

React useEffect hook handles the effects of the dependency array. It is called every time any state if the dependency array is modified or updated.



Syntax of useEffect Hook:

useEffect(() => {
// Effect code here
return () => {
// Cleanup code here (optional)
};
}, [dependencies]);

Approach to Fetch Data from API:

Fetching Random User Data

In this example, we’ll create a React component called RandomUserData that fetches random user data from the Random Data API. The component will utilize the useState and useEffect hooks to manage state and handle the data fetching process respectively. Once the data is fetched, it will be displayed on the screen.

Example: This example implements the above-mentioned approach




/* RandomUserData.js */
 
import React,
{
    useState,
    useEffect
} from 'react';
 
function RandomUserData() {
    const [userData, setUserData] = useState(null);
 
    useEffect(() => {
            .then(response => response.json())
            .then(data => setUserData(data));
    }, []);
 
    return (
        <div>
            {userData && (
                <div>
                    <h2>User Information</h2>
                    <p>
                        Name:
                        {userData.first_name}
                        {userData.last_name}
                    </p>
                    <p>
                        Email: {userData.email}
                    </p>
                    {/* Add more user data fields as needed */}
                </div>
            )}
        </div>
    );
}
 
export default RandomUserData;

Output:

Fetching Random User Data Output

Fetching Random User Data List

In this example, we’ll create a React component called RandomUserList that fetches a list of random users from the Random Data API. The component will utilize the useState and useEffect hooks to manage state and handle the data fetching process respectively. Once the data is fetched, it will be displayed on the screen as a list of users.

Example: This example implements the above-mentioned approach




/* RandomUserList.js */
import React,
{
    useState,
    useEffect
} from 'react';
 
function RandomUserList() {
    const [userList, setUserList] = useState([]);
 
    useEffect(() => {
            .then(response => response.json())
            .then(data => setUserList(data));
    }, []);
 
    return (
        <div>
            <h2>Random User List</h2>
            <ul>
                {userList.map(user => (
                    <li key={user.id}>
                        <p>
                            Name:
                            {user.first_name}
                            {user.last_name}
                        </p>
                        <p>
                            Email:
                            {user.email}
                        </p>
                        {/* Add more user data fields as needed */}
                    </li>
                ))}
            </ul>
        </div>
    );
}
 
export default RandomUserList;

Output:

Fetching Random User Data List Output


Article Tags :