ClientSide Data Fetching Using Next.js (original) (raw)

Client-Side Data Fetching Using Next.js

Last Updated : 10 Apr, 2026

Client-Side Data Fetching is a technique where data is fetched in the browser after the page loads, enabling dynamic and interactive user experiences. Unlike SSR and SSG, it shifts data handling to the client for better responsiveness.

client-side-rendering

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

Loading...

;

return (

Client-Side Fetched Posts

); }

`

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.

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

Loading...

; if (error) return

Failed to load data

;

return (

Posts (using SWR)

); }

`

Features of Client-Side Data Fetching