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. Faster initial page load.Real-time updates without refreshing the page.Reduced server load for static content.Client-side data fetching with useEffect and FetchThe 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 requestsAutomatic revalidation to keep data freshReal-time data updates without extra setupSimple and developer-friendly APIFirstly, install the swr in your Systemnpm 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 FetchingRuns 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. Create Quiz Comment A aman98 Follow 2 Improve A aman98 Follow 2 Improve Article Tags : Next.js react-js Explore Next js basicsNext.js Introduction6 min readGetting Started with Next JS9 min readNext.js Installation3 min readNextJS Folder Structure3 min readNext.js Create Next App3 min readNext js RoutingNext.js Routing6 min readNext.js Nested Routes4 min readNext.js Pages3 min readNext JS Layout Component3 min readNavigate Between Pages in NextJS3 min readLinking between pages in Next.js2 min readNext.js Redirects4 min readNext.js Dynamic Route Segments2 min readMiddlewares in Next.js6 min readNext JS Routing: Internationalization4 min readNext js Data FetchingNext.js Data Fetching6 min readServer Actions in Next.js3 min readData fetching in Next.js Using SSG and SSR5 min readNext js RenderingServer Components in Next.js4 min readEdge Functions and Middleware in Next JS3 min readHow to Reset Next.js Development Cache?3 min readNext js StylingAdd Stylesheet in Next.js4 min readControlling the specificity of CSS Modules in a Next.js App4 min readInstall & Setup Tailwind CSS with Next.js2 min readCSS-in-JS Next JS3 min readNext.js Styling: Sass3 min readNext js OptimizingNext.js Bundle Optimization to improve Performance6 min readNext JS Image Optimization3 min readLazy Loading in Next.js4 min readHow to Add Google Analytics to a Next.js Application?3 min readNext.js Static File Serving2 min readNext js ConfiguringNext.js TypeScript4 min readNext.js Environment Variables3 min readMDX in Next JS4 min readNext.js src Directory4 min readDraft Mode Next.js5 min readNext.js Security Headers6 min readUnit Testing in Next JS: Ensuring Code Quality in Your Project4 min readAdding user Authentication in Next.js using NextAuth3 min readNext js DeployingDeploying your Next.js App3 min readNext.js Static HTML Export5 min readMigrating from Create React App to NextJS: A Practical Guide4 min readNext js ComponentsNext.js Components4 min readFonts in Next JS6 min readNext js File ConventionsFile Conventions in Next.js5 min readNext JS File Conventions: default.js4 min readNext.js Layouts6 min readloading.js in Next JS3 min readNext JS File Conventions: not-found.js4 min readNext JS File Conventions: route.js3 min readtemplate.js in Next JS4 min readMetadata Files Next.js5 min read Like