Have you ever opened your browser’s Network tab and noticed that the same API is being called multiple times?
You fetch data when a page loads, but the same request runs again when you switch tabs, return to a page, or update a component. Sometimes, several API calls happen even when the user hasn’t done anything.
Choosing the right architecture and experienced React developers can help improve application performance and maintainability
This can make your application slower, increase backend load, and create unnecessary network traffic.
The good news? Many of these problems can be fixed by understanding how data fetching works in Next.js and React.
In this article, we’ll explore why unnecessary API calls happen and how to avoid them with practical examples.
1. Why Do Too Many API Calls Happen?
There isn’t always one single reason. Here are some common causes.
Problem 1: Fetching Data Inside a Component That Re-renders
Consider this example:
"use client";
import { useEffect, useState } from "react";
export default function UsersPage() {
const [users, setUsers] = useState([]);
useEffect(() => {
fetch("/api/users")
.then((res) => res.json())
.then(setUsers);
});
return <div>{users.length} users found</div>;
} Can you spot the problem?
The useEffect has no dependency array. It runs after every render. Because setUsers updates the state, the component renders again, which triggers another request.
This can create a repeated request loop.
The fix: Add an appropriate dependency array.
useEffect(() => {
fetch("/api/users")
.then((res) => res.json())
.then(setUsers);
}, []); Now the effect runs after the component mounts, rather than after every render.
However, this does not guarantee that the request will happen exactly once in every development or production scenario. React Strict Mode can run an additional effect setup cycle in development, and components can mount again during navigation.
Problem 2: Fetching Data from Multiple Components
Imagine a dashboard with three components:
- User Profile
- Recent Orders
- Account Summary
Suppose all three components need the same user information. If each component independently requests /api/user, the same data may be fetched multiple times.
This becomes a bigger problem when an application has many pages and reusable components.
The fix: Decide where shared data should be fetched.
In the Next.js App Router, you can fetch data in a shared Server Component and pass the required information to its children. For client-side data, a suitable caching library can help reuse previously fetched results.
The goal is not to put every request in one giant component. It is to avoid fetching identical data independently when it can be shared safely.
Problem 3: Using Client Components for Everything
In Next.js, adding “use client” makes a component part of the client-side component boundary.
Client Components are useful when you need browser APIs, event handlers, or interactive state. But making an entire page a Client Component when most of it only displays data can add unnecessary client-side JavaScript and complicate data fetching.
Selecting the right architecture is an important part of building scalable applications. Explore our web development services to learn more.
The fix: Use Server Components for content that can be rendered on the server, and Client Components for the interactive parts.
For example:// app/users/page.jsx
async function getUsers() {
const response = await fetch(
"https://example.com/api/users"
);
if (!response.ok) {
throw new Error("Failed to fetch users");
}
return response.json();
}
export default async function UsersPage() {
const users = await getUsers();
return (
<main>
<h1>Users</h1>
{users.map((user) => (
<p key={user.id}>{user.name}</p>
))}
</main>
);
} This example fetches data in a Server Component without using useEffect.
In a real application, handle errors appropriately and keep private credentials on the server. Also, use the correct cache settings for the freshness your data requires.
Problem 4: Making API Calls One After Another
Suppose a dashboard needs three independent pieces of data:
- User details
- Notifications
- Recent activities
A sequential approach looks like this:
const user = await getUser();
const notifications = await getNotifications();
const activities = await getActivities();
The second request waits for the first, and the third waits for the second.
When these requests are independent, this creates unnecessary waiting time.
The fix: Run independent requests in parallel.
const [user, notifications, activities] =
await Promise.all([
getUser(),
getNotifications(),
getActivities(),
]); Now the requests can run concurrently, reducing the total waiting time in many cases.
Keep in mind that Promise.all() rejects if any promise rejects. If some sections can still work when another fails, consider handling each request independently with Promise.allSettled() or appropriate error handling.
Problem 5: Refetching Data Without a Clear Reason
Sometimes an API is called again whenever a user revisits a page or switches between views, even though the data has not changed.
Whether this is a problem depends on the data.
For example, a public list of categories may not need to be fetched on every visit. A bank balance or live order status may require fresher data.
The fix: Choose a caching and revalidation strategy based on the data.
For server-side fetching in the Next.js App Router, you can configure caching and revalidation where appropriate. For client-side data, libraries such as TanStack Query and SWR support caching, request deduplication, and configurable refetching.
Don’t cache everything blindly. User-specific or sensitive responses need particular care to prevent data from being shared across users.
2. How to Identify Unnecessary API Calls
Before changing your code, find out what is actually happening.
Open your application in the browser and follow these steps:
- Open Developer Tools.
- Go to the Network tab.
- Filter requests by Fetch/XHR.
- Reload the page.
- Look for repeated requests to the same endpoint.
- Check when each request happens and what triggers it.
Ask yourself:
- Is the same endpoint called several times?
- Does a component render trigger another request?
- Are several components fetching identical data?
- Does navigation trigger a refetch?
- Is the application refetching data more frequently than the feature requires?
Remember that not every repeated request is a bug. Some requests are expected because of navigation, retries, polling, or development behavior.
3. A Better Approach to Data Fetching in Next.js
For most applications, start with these principles:
Use Server Components when appropriate. Fetch data on the server for pages that don’t need client-side interaction to load it.
Use Client Components when necessary. Keep interactive functionality on the client instead of moving the entire page there.
Share data thoughtfully. Avoid independent requests for the same data when a shared fetch or cache can safely serve it.
Run independent requests in parallel. Don’t make one request wait for another unless there is a real dependency.
Choose the right caching strategy. Consider whether the data is static, frequently updated, or specific to a particular user.
Using the right frameworks and modern web technologies can help create efficient, maintainable web applications.
Measure before optimizing. Use the Network tab and application monitoring to confirm that a change actually reduces unnecessary requests.
4. Final Thoughts
Too many API calls are not always caused by a bad backend. Sometimes, the problem is how the frontend fetches, shares, and refreshes data.
Understanding React rendering, Next.js Server and Client Components, parallel requests, and caching can help you build applications that are more efficient and easier to maintain.
The next time your Next.js application feels slow, don’t immediately assume the API is the problem.
Looking to build or improve a scalable application? Explore our custom software development services.
Open the Network tab, inspect the requests, find the cause, and fix the actual issue.