The Wayback Machine - https://web.archive.org/web/20251215175743/https://www.geeksforgeeks.org/nextjs/client-side-data-fetching/
Open In App

Client-Side Data Fetching Using Reactjs and Next.js

Last Updated : 02 Sep, 2025
Comments
Improve
Suggest changes
2 Likes
Like
Report

In modern web applications, client-side data fetching plays an important role in delivering dynamic and interactive user experiences. This approach fetches data in the browser after the initial page has loaded, unlike SSG where data is prepared at build time or SSR where data is fetched on every request. With the help of React hooks (useEffect) and libraries like SWR in Next.js, client-side fetching ensures faster page loads, real-time updates, and reduced server dependency for static content.

client-side-rendering
  • Faster initial page load.
  • Real-time updates without refreshing the page.
  • Reduced server load for static content.

Client-side data fetching with useEffect and Fetch

The simplest way to fetch data on the client side is using React’s useEffect hook.

JavaScript
import { useState, useEffect } from "react";

export default function ClientFetchExample() {
  const [posts, setPosts] = useState([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetch("https://jsonplaceholder.typicode.com/posts")
      .then((res) => res.json())
      .then((data) => {
        setPosts(data);
        setLoading(false);
      });
  }, []);

  if (loading) return <p>Loading...</p>;

  return (
    <div>
      <h1>Client-Side Fetched Posts</h1>
      <ul>
        {posts.slice(0, 5).map((post) => (
          <li key={post.id}>{post.title}</li>
        ))}
      </ul>
    </div>
  );
}

In this example:

  • Data is fetched after the page loads.
  • The loading state shows feedback until data arrives.

Client-side data fetching with SWR

Next.js recommends using the SWR library for client-side data fetching. It provides features like caching, revalidation, and automatic updates.

  • Built-in caching for faster repeated requests
  • Automatic revalidation to keep data fresh
  • Real-time data updates without extra setup
  • Simple and developer-friendly API

Firstly, install the swr in your System

npm install swr
JavaScript
import useSWR from "swr";

const fetcher = (url) => fetch(url).then((res) => res.json());

export default function SWRExample() {
  const { data, error, isLoading } = useSWR(
    "https://jsonplaceholder.typicode.com/posts",
    fetcher
  );

  if (isLoading) return <p>Loading...</p>;
  if (error) return <p>Failed to load data</p>;

  return (
    <div>
      <h1>Posts (using SWR)</h1>
      <ul>
        {data.slice(0, 5).map((post) => (
          <li key={post.id}>{post.title}</li>
        ))}
      </ul>
    </div>
  );
}

In this example :

  • The fetcher function fetches data from the given API and returns it as JSON.
  • The useSWR hook calls this fetcher, manages loading/error states, and provides the API response.
  • The component shows a loading message first, then displays the first 5 post titles once the data is ready.

Features of Client-Side Data Fetching

  • Runs in the Browser – Data is requested after the initial page has loaded.
  • Asynchronous Updates – UI updates automatically when new data arrives.
  • Loading & Error States – Easy to show feedback (e.g., spinners, error messages) while fetching.
  • Supports Real-Time Data – Ideal for live dashboards, chats, or stock updates.
  • API Flexibility – Works with REST APIs, GraphQL, or any external service.
  • Lightweight on Server – Server delivers static content, while the browser handles data requests.
  • Integrates with Libraries – Tools like SWR or React Query add caching, revalidation, and better performance.

Article Tags :

Explore